Print a variable in Hexadecimal in Python

avatar

Borislav Hadzhiev

Last updated: Sep 4, 2022

banner

Photo from Unsplash

Print a variable in Hexadecimal in Python #

Use the hex() function to print a variable in hexadecimal, e.g. print(hex(variable)). If the variable stores a string, iterate over it and pass the Unicode code point of each character to the hex() function.

main.py
my_str = 'example' result = ' '.join(hex(ord(char)) for char in my_str) print(result) # 👉️ 0x65 0x78 0x61 0x6d 0x70 0x6c 0x65 # ------------------------------------------ result = ' '.join(f'{ord(char):x}' for char in my_str) print(result) # 👉️ 65 78 61 6d 70 6c 65 # ------------------------------------------ # ✅ Convert bytes to hex my_bytes = b'\x61\x70\x6c\x65' result = my_bytes.hex() print(result) # 👉️ 61706c65 result = ' '.join(hex(i) for i in my_bytes) print(result) # 👉️ 0x61 0x70 0x6c 0x65 # ------------------------------------------ # ✅ Get hexadecimal representation of an integer my_int = 90 result = hex(my_int) print(result) # 👉️ 0x5a # ------------------------------------------ # ✅ Convert list of integers to hexadecimal string my_list = [3, 5, 90, 150, 185] result = ''.join(f'{num:02x}' for num in my_list) print(result) # 👉️ 03055a96b9

The first two examples print a string variable in hexadecimal.

main.py
my_str = 'example' result = ' '.join(hex(ord(char)) for char in my_str) print(result) # 👉️ 0x65 0x78 0x61 0x6d 0x70 0x6c 0x65 result = ' '.join(f'{ord(char):x}' for char in my_str) print(result) # 👉️ 65 78 61 6d 70 6c 65

We used a generator expression to iterate over the string.

Generator expressions are used to perform some operation for every element or select a subset of elements that meet a condition.

On each iteration, we pass the current character to the ord() function to get the corresponding Unicode code point.

main.py
print(ord('a')) # 👉️ 97 print(ord('b')) # 👉️ 98

The ord function takes a string that represents 1 Unicode character and returns an integer representing the Unicode code point of the given character.

The hex function converts an integer to a lowercase hexadecimal string prefixed with 0x.

main.py
my_str = 'example' result = ' '.join(hex(ord(char)) for char in my_str) print(result) # 👉️ 0x65 0x78 0x61 0x6d 0x70 0x6c 0x65

If you don't need to prefix the hex string with 0x, use a formatted string literal.

main.py
my_str = 'example' result = ' '.join(f'{ord(char):x}' for char in my_str) print(result) # 👉️ 65 78 61 6d 70 6c 65

Formatted string literals (f-strings) let us include expressions inside of a string by prefixing the string with f.

The x character after the colon stands for hex format. It outputs the number before the colon in base 16, using lowercase letters for the digits above 9.

The examples join the generator object with a space separator, but you can use any other separator, e.g. a colon.

main.py
my_str = 'example' result = ':'.join(hex(ord(char)) for char in my_str) print(result) # 👉️ 0x65:0x78:0x61:0x6d:0x70:0x6c:0x65 result = ':'.join(f'{ord(char):x}' for char in my_str) print(result) # 👉️ 65:78:61:6d:70:6c:65

You can use the same approach if you need to convert a bytes object to hex and print the result.

main.py
my_bytes = b'\x61\x70\x6c\x65' result = my_bytes.hex() print(result) # 👉️ 61706c65 result = ' '.join(hex(i) for i in my_bytes) print(result) # 👉️ 0x61 0x70 0x6c 0x65

The bytes.hex method returns a string that contains two hexadecimal digits for each byte.

If you need to print a variable that stores an integer in hexadecimal, pass it to the hex() function.

main.py
my_int = 90 result = hex(my_int) print(result) # 👉️ 0x5a

If you need to convert a list of integers to a hex string, use a generator expression and a formatted string literal.

main.py
my_list = [3, 5, 90, 150, 185] result = ' '.join(f'{num:02x}' for num in my_list) print(result) # 👉️ 03 05 5a 96 b9

The :02x syntax is used to pad each value with leading zeros to a fixed width of 2.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.