Printing Integers as Hexadecimal in Python
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().
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.
Related Commands and Tools
These complementary Python tools and commands are useful for daily development workflows:
- python3 -m venv myenv – Create an isolated virtual environment
- pip list –outdated – Check which packages need updating
- python3 -m py_compile script.py – Check syntax without running
- black script.py – Auto-format code to PEP 8 standards
- mypy script.py – Static type checking for Python code
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.
