Printing Integers in Hexadecimal Format in Python
Using hex() for basic conversion
The hex() function converts an integer to a hexadecimal string with a 0x prefix:
i = 255
print(hex(i)) # Output: 0xff
This works for negative numbers too:
print(hex(-42)) # Output: -0x2a
String formatting for control
For more control over the output, use f-strings or the format() method with format specifiers:
i = 255
print(f"Value: {i:x}") # Output: Value: ff
print(f"Value: {i:X}") # Output: Value: FF
print(f"Value: {i:#x}") # Output: Value: 0xff
print(f"Value: {i:08x}") # Output: Value: 000000ff
The format specifiers work as follows:
:x— lowercase hex without prefix:X— uppercase hex without prefix:#x— lowercase hex with0xprefix:#X— uppercase hex with0Xprefix:08x— zero-padded to 8 characters
F-strings are the modern approach and more readable than .format():
# Older style (still works)
print("Hex: {}".format(hex(255)))
# Modern style
print(f"Hex: {255:x}")
Padding and width specifications
When working with fixed-width values like color codes or memory dumps, padding is essential:
values = [15, 255, 4095]
for val in values:
print(f"{val:04x}")
# Output:
# 000f
# 00ff
# 0fff
Use :0Nx where N is the total width. This zero-pads on the left.
Converting hex strings back to integers
Use int() with base 16 to convert hex strings back:
hex_string = "ff"
decimal = int(hex_string, 16)
print(decimal) # Output: 255
# Works with 0x prefix too
decimal = int("0xff", 16)
print(decimal) # Output: 255
Practical examples
Formatting IP octets as hex:
ip_octets = [192, 168, 1, 1]
hex_ip = ".".join(f"{octet:02x}" for octet in ip_octets)
print(hex_ip) # Output: c0.a8.01.01
Debugging binary data or memory values:
data = bytes([0x48, 0x65, 0x6c, 0x6c, 0x6f])
print(" ".join(f"{byte:02x}" for byte in data))
# Output: 48 65 6c 6c 6f
Color codes:
rgb = (255, 100, 50)
hex_color = f"#{rgb[0]:02x}{rgb[1]:02x}{rgb[2]:02x}"
print(hex_color) # Output: #ff6432
Edge cases
For very large integers, Python handles them natively:
large = 2**64 - 1
print(f"{large:x}") # Output: ffffffffffffffff
Negative numbers with hex formatting require caution. The format specifier applies to the magnitude, but the minus sign is prepended:
print(f"{-255:x}") # This will raise ValueError
print(f"{-255:#x}") # This will raise ValueError
print(hex(-255)) # Output: -0xff (use hex() instead)
If you need to handle negatives consistently, convert to unsigned (two’s complement) first if necessary, or stick with hex().