Python hex() Built in Function

The hex() function in Python is a built-in function that converts an integer to its corresponding hexadecimal number and returns it as a string. The hexadecimal representation is in lowercase prefixed with 0x. For example:

>>> hex(255) # convert 255 to hexadecimal
'0xff

>>> hex(-35) # convert -35 to hexadecimal
'-0x23'

>>> type(hex(10)) # check the return type of hex()
<class 'str'>

You can also use the float.hex() method to convert a floating-point number to its hexadecimal representation. For example:

>>> float.hex(2.5) # convert 2.5 to hexadecimal
'0x1.4000000000000p+1'

>>> float.hex(0.0) # convert 0.0 to hexadecimal
'0x0.0p+0'

If you want to learn more about Python built-in functions, you can check out some of the web pages on Python hex() function.

If you have any questions about this code, you can drop a line in comment.

Comments

Popular posts from this blog

Python chr() Built in Function

Python float() Built in Function

Python bool() Built in Function

Python Printing Readable Results

Python exec() Built in Function