Python Tutorial Python Advanced Python Resources Python Libraries
Python Java C++ C C# PHP R SQL DS Algo InterviewQ

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

5