Print a string as hexadecimal bytes in Python

avatar

Borislav Hadzhiev

Last updated: Sep 4, 2022

banner

Photo from Unsplash

Print a string as hexadecimal bytes in Python #

To print a string as hexadecimal bytes:

  1. Use a generator expression to iterate over the string.
  2. Pass the Unicode code point of each character to the hex() function.
  3. Use the str.join() method to join the generator object into a string.
main.py
my_str = 'apple' result = ':'.join(hex(ord(char)) for char in my_str) print(result) # 👉️ 0x61:0x70:0x70:0x6c:0x65 result = ':'.join(f'{ord(char):x}' for char in my_str) print(result) # 👉️ 61:70: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 = 'apple' result = ':'.join(hex(ord(char)) for char in my_str) print(result) # 👉️ 0x61:0x70:0x70:0x6c:0x65

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

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

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

Make sure to wrap expressions in curly braces - {expression}.

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.

You can read more about the format specification mini-language in this section of the docs.

The examples above join the hexadecimal strings with a colon separator, but you can use any other separator, e.g. a space.

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

The str.join method takes an iterable as an argument and returns a string which is the concatenation of the strings in the iterable.

The string the method is called on is used as the separator between the elements.

If you don't need a separator and just want to join the iterable's elements into a string, call the join() method on an empty string.

main.py
my_str = 'apple' result = ''.join(hex(ord(char)) for char in my_str) print(result) # 👉️ 0x610x700x700x6c0x65 result = ''.join(f'{ord(char):x}' for char in my_str) print(result) # 👉️ 6170706c65
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.