Python Tutorial

Python hex()

Guide to python hex(): usage, examples, __index__ support, negative numbers, and alternatives (format(), f-strings).

Drake Nguyen

Founder · System Architect

3 min read
Python hex()
Python hex()

Overview of python hex()

The python hex() built-in function converts an integer to its hexadecimal string representation. The returned string includes the base prefix 0x and uses lowercase letters for the digits a–f. You can pass integers written in any base (binary, octal, decimal, hexadecimal) and hex() will produce the standard hexadecimal string.

Simple examples: convert int to hex python

Basic uses demonstrate how hex() converts integers from different literal formats:

print(hex(255))        # decimal -> '0xff'
print(hex(0b11111111)) # binary  -> '0xff'
print(hex(0o377))      # octal   -> '0xff'
print(hex(0xFF))       # hex     -> '0xff' (normalized to lowercase)

Behavior with negative numbers

hex() preserves the sign for negative integers: the minus sign appears before the 0x prefix.

print(hex(-255))  # '-0xff'

Using hex() with objects and __index__

hex() accepts objects that implement the __index__() method. That method must return an integer; hex() will call it and convert the returned integer to hexadecimal. If an object lacks __index__, calling hex(obj) raises a TypeError.

class MyIntLike:
    def __init__(self, value):
        self.value = value
    def __index__(self):
        return self.value

obj = MyIntLike(100)
print(hex(obj))  # '0x64'

hex() output format and lowercase behavior

  • Prefix: hex() always includes the 0x prefix.
  • Case: letters a–f are returned in lowercase (e.g., 0xff).
  • Type: hex() returns a Python str, not an integer.

Alternatives: format() and f-strings

If you need different formatting options, consider format() or f-strings.

# lowercase without '0x'
print(format(255, 'x'))    # 'ff'

# include prefix
print(format(255, '#x'))   # '0xff'

# uppercase hex digits
print(format(255, '#X'))   # '0XFF'

# f-string examples
print(f"{255:#x}")       # '0xff'
print(f"{255:X}")        # 'FF' (no prefix)

Use these when you want to control presence of the prefix or letter casing—a common alternative to python hex() when precise formatting is required.

Converting hexadecimal strings back to integers

To convert a hexadecimal string to an integer, use int() with base 16. This works with or without the 0x prefix.

print(int('ff', 16))    # 255
print(int('0xff', 16))   # 255

Quick tips and common pitfalls

  • hex() returns lowercase and includes 0x—use format() or f-strings to change case or omit the prefix.
  • Passing a non-integer object without __index__() triggers a TypeError.
  • To parse user input that may be hex, call int(value, 16) and handle ValueError for invalid strings.

References

Official Python documentation: https://docs.python.org/3/library/functions.html#hex

Example search terms: python hex(), hex() python, python hexadecimal, convert int to hex python, python hex() function example

Stay updated with Netalith

Get coding resources, product updates, and special offers directly in your inbox.