Python Integer to Hex String (Easy)

πŸ’¬ Question: How to convert an integer to a hex string in Python?

This short tutorial will show you four ways to accomplish this easily and effectively. Let’s dive into the first one right away! πŸ‘‡

Method 1: hex()

The easiest way to convert a decimal integer number x to a hexadecimal string is to use the built-in Python function hex(x). The return value of the hex() function is a string representation of the hexadecimal representation of the decimal input number—with the prefix '0x'. For example, hex(255) yields '0xff'.

Here are a couple of interesting examples that convert decimal integers to hexadecimal strings:

>>> hex(255)
'0xff'
>>> hex(15)
'0xf'
>>> hex(10)
'0xa'
>>> hex(11)
'0xb'
>>> hex(16)
'0x10'

There are some subtleties of this function that you may find interesting. Feel free to watch my explainer video tutorial:

🌍 Recommended Tutorial: Python hex() Function

Method 2: chr()

Python’s built-in function chr() takes a decimal number as an argument and returns the character associated with this number according to the Unicode table. For example, the call chr(101) returns the Unicode character 'e'.

The allowed range of arguments are all integers between 0 and 1,114,111 (included)β€”integers outside this interval will raise a ValueError.

You can pass a hexadecimal representation (e.g., 0x65 or 0x55 or 0xff) into chr() because Python will automatically convert the hex to an integer number—the canonical representation. Thus, each hex number can represent up to one Unicode symbol. And this symbol is also represented by a hex string such as '\x65' or '\x55' or '\xff'.

Thus, a Unicode character represented by a hex number is considered equal to the hex string encoding the same Unicode symbol!

>>> chr(0x65) == '\x65'
True
>>> chr(0x55) == '\x55'
True

For example, you can convert hexadecimal 0x55 to decimal 85, convert it to the associated character using chr(), and compare the result to the hex string '\x55'.

Both the character and the string are considered the same because they are considered to point to the same Unicode symbol.

See here:

>>> 0x55
85
>>> chr(85) == '\x55'
True

Here’s an example of the Unicode table:

Unicode characterDescriptionDec
Alatin capital letter a65
Blatin capital letter b66
Clatin capital letter c67
Dlatin capital letter d68
Elatin capital letter e69
Flatin capital letter f70

And here’s a video tutorial on the important chr() function:

🌍 Recommended Tutorial: Python chr() Function

Method 3: f-String with x Format Specifier

The f-string expression f'0x{my_int:x}' converts the integer value in variable my_int to a hex string using the prefix '0x' and the hexadecimal number defined with the lowercase x as format specifier after the colon :x.

Here’s an example:

my_int = 15
my_hex = f'0x{my_int:x}'
print(my_hex) 
# 0xf

Simply replace my_int with your specific variable name holding the integer value to be converted to a hex string.

🌍 Recommended Tutorial: f-Strings in Python

Also, check out my detailed tutorial on using the powerful string formatting capabilities in Python to convert an integer to a hex string.

Method 4: .format() String Method

Alternatively to the f-string, you can also use the '{:x}'.format(15) string method call with lowercase x to convert the integer 15 to a lowercase hex string. If you need uppercase, you can use the uppercase string formatting specifier X like so: '{:X}'.format(15).

Of course, replace the value 15 with your integer number to be converted to a hex string.

>>> '{:x}'.format(15)
'f'
>>> '{:x}'.format(255)
'ff'
>>> '{:x}'.format(256)
'100'

🌍 Recommended Tutorial: How to Print an Uppercase Hex String in Python?

Keep Learning! πŸ‘‡

Thanks for reading the whole tutorial! If you want to keep learning, feel free to join our free email academy. We have cheat sheets too!