Python Hexadecimal - How to convert an integer to the hexadecimal string

Python hex converts an integer to the hex string. For example, 19 is 16 + 3 so 19 is converted to 0x13. 0x is a prefix meaning the value is base 16.

a = hex(19)
b = format(19, 'x')

print(a)  # 0x13
print(type(a))  # <class 'str'>

print(b)  # 13
print(type(b))  # <class 'str'>

If you want to remove 0x prefix, format is the best way. format literally formats a value.

Example

a = hex(1023)
b = format(1023, 'x')

print(a)  # 0x3ff
print(type(a))  # <class 'str'>

print(b)  # 3ff
print(type(b))  # <class 'str'>

\[ 1023 = 16 \times 16 \times 3 + 16 \times 15 + 15 \]

Python String

Python Tutorial