hex
Convert an integer to hexadecimal notation.
Python 3.13
hex(x)Parameters
- xint
- An integer.
Return value
A string with the 0x prefix, such as "0xff".
Examples
Hexadecimal notation
Python 3.13
print(hex(255))0xff
Round trip with int
Python 3.13
code = hex(2024)
print(code, int(code, 16))0x7e8 2024
Details
The letters a–f are lowercase. format(x, "x") and format(x, "X") give the notation without a prefix or in uppercase. A string is converted back with int("ff", 16); the 0x prefix is also allowed with base=16.
