Last updated: Apr 9, 2024
Reading timeยท3 min
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.
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 first two examples print a string variable in hexadecimal.
We used a generator expression to iterate over the string.
On each iteration, we pass the current character to the ord()
function to get
the corresponding Unicode code point.
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
.
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
If you don't need to prefix the hex string with 0x
, use a formatted string
literal.
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
Formatted string literals
(f-strings) let us include expressions inside of a string by prefixing the
string with f
.
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.
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
You can use the same approach if you need to convert a bytes object to hex and print the result.
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.
If you need to print a variable that stores an integer in hexadecimal, pass it
to the hex()
function.
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.
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.
You can learn more about the related topics by checking out the following tutorials: