Format Integer List to Hex String – Six Pythonic Ways

Problem Formulation

πŸ’¬ Question: How to create a string of hex digits from a list of integers (0255) so that each hex digit consists of two digits such as "00", "01", …, "fe", "ff"?

Here’s an example input/output pair:

In:    [0, 1, 2, 3, 255, 254, 253]
Out:   '00010203fffefd'

Method 1: Bytearray

The easiest way to convert a list of ints to a hex string is to use the bytearray(ints) function to create a bytearray and call the bytearray(ints).hex() method to convert it to a hex string.

Here’s a simple example:

ints = [0, 1, 2, 3, 255, 254, 253]
my_hex = bytearray(ints).hex()

print(my_hex)
# 00010203fffefd

Now, if you’re like me you’re not too comfortable with byte arrays and how they compare to the simple byte type in Python. If you want to refresh your memory, feel free to check out our interesting tutorial:

🌍 Recommended Tutorial: Bytes vs Bytearrays in Python

Method 2: f-String

The expression ''.join(f'{i:02x}' for i in ints) converts each int from a list of ints into a single two-digit hex string using f-strings and combines all those 2-digit hex strings into a single big string using the string.join() method.

ints = [0, 1, 2, 3, 255, 254, 253]
my_hex = ''.join(f'{i:02x}' for i in ints)

print(my_hex)
# 00010203fffefd

You can learn more about the generator expression used in the join() method in our detailed tutorial here:

🌍 Recommended Tutorial: List Comprehension in Python — Ultimate Guide

Method 3: Percentage Operator

The expression ''.join('%02x'%i for i in ints) converts each int from a list of ints into a single two-digit hex string using the percentage formatting operator and combines all those 2-digit hex strings into a single big string using the string.join() method.

ints = [0, 1, 2, 3, 255, 254, 253]
my_hex = ''.join('%02x'%i for i in ints)

print(my_hex)
# 00010203fffefd

An interesting tutorial for you may be the following that shows how to use the different string formatting operators to convert an integer to a hexadecimal string—this is at the core of what we’re doing in the previous code snippet:

🌍 Recommended Tutorial: Python Int to Hex | String Formatting

Method 4: string.format()

The expression ''.join('{:02x}'.format(i) for i in ints) converts each int from a list of ints into a single two-digit hex string using the string.format() method and combines all those 2-digit hex strings into a single big string using the string.join() method.

ints = [0, 1, 2, 3, 255, 254, 253]
my_hex = ''.join('{:02x}'.format(i) for i in ints)

print(my_hex)
# 00010203fffefd

I have created a whole tutorial on the string.format() method—check it out here.

Method 5: format()

The expression ''.join(format(i, '02x') for i in ints) converts each int from a list of ints into a single two-digit hex string using the built-in Python format() function and combines all those 2-digit hex strings into a single big string using the string.join() method.

ints = [0, 1, 2, 3, 255, 254, 253]
my_hex = ''.join(format(i, '02x') for i in ints)

print(my_hex)
# 00010203fffefd

I actually prefer this approach if you don’t want to use f-strings because it’s clean, and there is little unnecessary syntactical sugar like the percentage symbol or brackets or anything.

You can learn more about the built-in function here:

🌍 Recommended Tutorial: Python format() Function: No-BS Guide by Example

Bonus Method 6: Use More Hex Digits Per Integer

What if you want to use more than two digits per hexadecimal string that was the result of the conversion from an individual integer in the list?

Say, 3 digits per hex string?

To use more than two digits per hex string when converting a list of ints to a single string of n-digit hex numbers, use the format specifier 0nx, replacing n with the number of digits you want.

  • 0 stands for “pad 0 symbols to the left to ensure that all hex strings have the same width”
  • n stands for “the width of the hex string after conversion”. This is how many 0s are added so that each hex string has the same number of digits.
  • x stands for “converting to a hexadecimal string representation”. You could also convert to other numerical systems such as binary or octal. But we won’t here. πŸ˜‰

For example, the expression ' '.join(f'{i:03x}' for i in ints) may yield this hex string '000 001 002 003 0ff 0fe 0fd':

ints = [0, 1, 2, 3, 255, 254, 253]
my_hex = ' '.join(f'{i:03x}' for i in ints)

print(my_hex)
# 000 001 002 003 0ff 0fe 0fd

There are more ways to convert an integer to a hexadecimal number. I could come up with arbitrary many combinations of methods by just varying different approaches—but I think you’ll get the idea.

If you want to learn more methods to use as an expression within the generator expression of all previous code snippets, check out this tutorial. πŸ‘‡

🌍 Recommended Tutorial: Convert Int to Hex

Also, you may want to check out this interesting tutorial:

🌍 Recommended Tutorial: Easiest Way to Convert List of Hex Strings to List of Integers

Finally, feel free to join our free email academy and download our cheat sheets: