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.
Decimal | Hexadecimal | Decimal | Hexadecimal |
---|---|---|---|
1 | 1 | 10 | a |
2 | 2 | 11 | b |
3 | 3 | 12 | c |
4 | 4 | 13 | d |
5 | 5 | 14 | e |
6 | 6 | 15 | f |
7 | 7 | 16 | 10 |
8 | 8 | 31 | 1f |
9 | 9 | 255 | ff |
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