ID EN
Built-in Functions

hex

Python 3.11

Convert an integer number to a lowercase hexadecimal string prefixed with “0x”. If x is not a Python int object, it has to define an __index__() method that returns an integer. Some examples:

Syntax

PYTHON
hex(x)

Examples

Example 1
PYTHON
>>> hex(255)
'0xff'
>>> hex(-42)
'-0x2a'
Example 2
PYTHON
>>> '%x' % 255, '%x' % 255, '%X' % 255
('0xff', 'ff', 'FF')
>>> format(255, 'x'), format(255, 'x'), format(255, 'X')
('0xff', 'ff', 'FF')
>>> f'{255:x}', f'{255:x}', f'{255:X}'
('0xff', 'ff', 'FF')

See Also

int __index__() format() int() float.hex()