Python Glossary Русский

all Python 3Built-in functionsterm

hex

numbers

Converts an integer to its hexadecimal string representation with the '0x' prefix. The letters are lower-case.

Syntax

hex(x)

The digits are always lower-case and the 0x prefix is always there — for upper-case or prefix-free output use the x and X format codes. The function accepts integers only: floats have their own float.hex() method, and byte data is converted by bytes.hex(). To parse it back use int(s, 16), which accepts the string with or without the 0x prefix.

Examples

print(hex(255))  # → 0xff
print(hex(16))   # → 0x10
print(hex(-42))  # → -0x2a
print(hex(0))    # → 0x0
print(int(hex(255), 16))  # → 255

See also

Open in the full glossarydocs.python.orgReport an inaccuracy