# How to Print a Variable in Hexadecimal in Python

Last updated: Apr 9, 2024
3 min

## #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
```Copied!```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.

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
```Copied!```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
```Copied!```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
```Copied!```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`.

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
```Copied!```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
``````

## #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
```Copied!```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
```Copied!```my_int = 90

result = hex(my_int)
print(result)  # ๐๏ธ 0x5a
``````

## #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
```Copied!```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.