Skip to content

How to Print Variables in Hexadecimal Format in Python

Learn how to convert and display integers, strings, bytes, and lists as hexadecimal values in Python using hex(), f-strings, and generator expressions.

This guide explains how to print variables in hexadecimal format (base-16) in Python. We'll cover how to print integers, strings, bytes objects, and lists of integers in hexadecimal, using built-in functions like hex(), f-strings, and generator expressions.

Printing Integers in Hexadecimal

Using hex() (with "0x" prefix)

The built-in hex() function converts an integer to a lowercase hexadecimal string, prefixed with "0x":

my_int = 90
result = hex(my_int)
print(result)  # Output: 0x5a
  • hex(my_int): This converts the integer my_int to its hexadecimal string representation.

Using f-strings (without "0x" prefix)

For more control over the output format (and to omit the 0x prefix), use f-strings:

my_int = 90
result = f'{my_int:x}'  # Lowercase hex, no prefix
print(result)           # Output: 5a

result = f'{my_int:X}'  # Uppercase hex, no prefix
print(result)           # Output: 5A
  • f'{my_int:x}': Formats my_int as a lowercase hexadecimal string.
  • f'{my_int:X}': Formats my_int as an uppercase hexadecimal string.

Padding with Leading Zeros

To pad the hexadecimal representation with leading zeros, specify a width in the f-string:

my_int = 90
result = f'{my_int:04x}' # Lowercase hex, padded to 4 digits
print(result)            # Output: 005a

my_int = 15
result = f'{my_int:04X}' # Uppercase hex, padded to 4 digits
print(result)            # Output: 000F
  • f'{my_int:04x}': Pads with leading zeros to a width of 4, using lowercase hex.
  • f'{my_int:04X}': Pads with leading zeros to a width of 4, using uppercase hex.

Printing Strings in Hexadecimal

To represent a string as a sequence of hexadecimal values (representing the Unicode code points of each character), you need to iterate over the string:

my_str = 'tutorialreference.com'

result = ' '.join(hex(ord(char)) for char in my_str)  # with 0x prefix
print(result)

result = ' '.join(f'{ord(char):x}' for char in my_str) # without 0x prefix
print(result) 
  • ord(char): Gets the Unicode code point (an integer) of each character.
  • hex(ord(char)): Converts the integer code point to its hexadecimal representation (with "0x" prefix).
  • f'{ord(char):x}': Formats each character's Unicode code point as lowercase hex without the 0x prefix.
  • ' '.join(...): Joins the hexadecimal representations with spaces.

Printing Bytes Objects in Hexadecimal

For bytes objects, you can use the .hex() method directly:

my_bytes = b'tutorialreference.com'
print(my_bytes.hex()) 

my_str = 'tutorialreference.com'

result = ':'.join(hex(ord(char)) for char in my_str)
print(result) 

result = ':'.join(f'{ord(char):x}' for char in my_str)
print(result) 
  • The bytes.hex() method returns a string containing two hexadecimal digits for each byte.

Printing Lists of Integers in Hexadecimal

To format a list of integers as hexadecimal strings, use a list comprehension or a generator expression with f-strings:

my_list = [3, 5, 90, 150, 185]
result = ' '.join(f'{num:02x}' for num in my_list)  # Lowercase, padded
print(result)  # Output: 03 05 5a 96 b9
  • f'{num:02x}': Formats each number as lowercase hex, padded with a leading zero to a width of 2.
Navigation