SysTutorials

Printing Integers as Hexadecimal in Python

· updated April 12, 2026

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:

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().

Common Pitfalls and Best Practices

When working with Python on Linux systems, keep these considerations in mind. Always use virtual environments to avoid polluting the system Python installation. Python 2 reached end-of-life in 2020, so ensure you are using Python 3 for all new projects.

For system scripting, prefer the subprocess module over os.system for better control over process execution. Use pathlib instead of os.path for cleaner file path handling in modern Python.

These complementary Python tools and commands are useful for daily development workflows:

Quick Verification

After applying the changes described above, verify that everything works as expected. Run the relevant commands to confirm the new configuration is active. Check system logs for any errors or warnings that might indicate problems. If something does not work as expected, review the steps carefully and consult the official documentation for your specific version.

← All posts