How to Print a Variable in Hexadecimal in Python

avatar
Borislav Hadzhiev

Last updated: Apr 9, 2024
3 min

banner

# 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 = 'bobbyhadz.com' result = ' '.join(hex(ord(char)) for char in my_str) print(result) # ๐Ÿ‘‰๏ธ 0x62 0x6f 0x62 0x62 0x79 0x68 0x61 0x64 0x7a 0x2e 0x63 0x6f 0x6d # ------------------------------------------ result = ' '.join(f'{ord(char):x}' for char in my_str) print(result) # ๐Ÿ‘‰๏ธ 62 6f 62 62 79 68 61 64 7a 2e 63 6f 6d

print variable in hexadecimal

The code for this article is available on GitHub

The first two examples print a string variable in hexadecimal.

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 = 'bobbyhadz.com' result = ' '.join(hex(ord(char)) for char in my_str) print(result) # ๐Ÿ‘‰๏ธ 0x62 0x6f 0x62 0x62 0x79 0x68 0x61 0x64 0x7a 0x2e 0x63 0x6f 0x6d

# Get hex representation without 0x

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

main.py
my_str = 'bobbyhadz.com' result = ' '.join(f'{ord(char):x}' for char in my_str) print(result) # ๐Ÿ‘‰๏ธ 62 6f 62 62 79 68 61 64 7a 2e 63 6f 6d

get hex representation without 0x

The code for this article is available on GitHub

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.

# Using a different separator

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 = 'bobbyhadz.com' 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
The code for this article is available on GitHub

# Converting a bytes object to hex

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

main.py
my_str = 'bobbyhadz.com' result = ':'.join(hex(ord(char)) for char in my_str) print(result) # ๐Ÿ‘‰๏ธ 0x62:0x6f:0x62:0x62:0x79:0x68:0x61:0x64:0x7a:0x2e:0x63:0x6f:0x6d result = ':'.join(f'{ord(char):x}' for char in my_str) print(result) # ๐Ÿ‘‰๏ธ 62:6f:62:62:79:68:61:64:7a:2e:63:6f:6d

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

# Printing a variable that stores an integer in hexadecimal

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
The code for this article is available on GitHub

# Converting a list of integers to a hex string

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.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

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.

Copyright ยฉ 2024 Borislav Hadzhiev