Python Tutorial Python Advanced Python References Python Libraries

Python hex() Function



The Python hex() function is used to convert a specified number (positive or negative integers only) into hexadecimal numeral system. It returns a hexadecimal string which always starts with 0x followed by hexadecimal value of the specified number.

Syntax

hex(number)

Parameters

number Required. Specify a positive or negative integer.

The table below shows decimal numbers converted into hexadecimal numbers.

DecimalHexadecimalDecimalHexadecimal
1110a
2211b
3312c
4413d
5514e
6615f
771610
88311f
99255ff

Example:

The example below shows how to convert decimal number into hexadecimal number in Python.

MyNumber = hex(31)
print(MyNumber)

MyNumber = hex(255)
print(MyNumber)

The output of the above code will be:

0x1f
0xff

❮ Python Built-in Functions