hex()

Convert an integer to its hexadecimal literal form — a string, not a number.

Built-in functionPython 1.0+Live demo
Common call
hex(255) # "0xff"
Returns
a str with "0x" prefix, lowercase digits
Replaces
the format(n, "x") family when you also want the prefix
Watch out
floats are rejected — use int() first or float.hex() instead
hex(xxAny integer (positive, negative, or zero). Objects with __index__ also accepted. Floats raise TypeError.type: int · required)
str

Demo

Live evaluation
Try:
Inputs
xintthe integer
Output
hex(255)
'0xff'

hex returns a STRING in the same form you would type an integer literal: "0x" prefix, lowercase digits. Negatives get a leading "-". This is a formatter, not a math operation — the value does not change, only its representation.

Parameters

NameTypeRequiredDescription
xintyesAny integer (positive, negative, or zero). Objects with __index__ also accepted. Floats raise TypeError.

Return value

strA string in Python integer literal form: lowercase digits with a "0x" prefix, and "-0x" for negatives.

Common patterns

Display bytes as hex
Round-trippable literal form — useful in error messages and logs.
print(f"got byte {hex(b)}")
Hex without the prefix
When you only want the digits, format is cleaner and lets you control padding.
digits = format(255, "x")            # "ff"
padded = format(255, "04x")           # "00ff"
Round-trip via int
hex → str, int with base=16 → back. The prefix is optional on parse.
s = hex(255)         # "0xff"
n = int(s, 16)       # 255

Examples

1. Small integer
hex(255)
Returns
"0xff"
2. Zero
hex(0)
Returns
"0x0"
3. Negative
hex(-255)
Returns
"-0xff"
4. Big number
hex(16 ** 5)
Returns
"0x100000"
5. Float raises
hex(1.5)
Returns
TypeError: 'float' object cannot be interpreted as an integer

Pitfalls

1. The output includes the "0x" prefix
For humans reading Python literals, that is exactly right. When you are building a fixed-width dump or a hex color, the prefix is in the way.
Prefix not wanted
color = "#" + hex(255)
"#0xff" # extra 0x
Use format
color = "#" + format(255, "02x")
"#ff"
2. Floats are rejected
hex only accepts integers. For float bit-patterns there is float.hex, which returns a different (IEEE 754 hex) form.
Type error
hex(1.5)
TypeError: 'float' object cannot be interpreted as an integer
Truncate first
hex(int(1.5))
"0x1"
3. Result is a str, not an int
You cannot do arithmetic on a hex string — Python does not auto-convert.
Type error
hex(15) + 1
TypeError: can only concatenate str (not "int") to str
Parse back
int(hex(15), 16) + 1
16
4. Confused with .hex() on bytes
hex(x) is for INTEGERS. bytes.hex() is a method on bytes objects returning a hex string of the raw bytes — no prefix, all digits.
Wrong tool
hex(b"\xff\x00")
TypeError: 'bytes' object cannot be interpreted as an integer
Method form
b"\xff\x00".hex()
"ff00"

When to use

Use it
  • Human-readable Python-literal display of an integer
  • Round-trippable output that int(s, 16) can parse back
  • Diagnostic messages and logs where the prefix aids reading
Reach for something else
  • No prefix wanted → format(n, "x")
  • Fixed width / padding → format(n, "0Nx")
  • Bytes → bytes.hex()
  • Floats → float.hex() (different format entirely)

Notes

Complexity
O(log n) in the number of digits
Return
str — always lowercase, always prefixed with "0x" (or "-0x")
CPython impl
Python/bltinmodule.c :: builtin_hex — delegates to type's __index__ then formats
Memory
Allocates one small string
Thread-safe
Yes — a pure computation

FAQ

Use format() or f-string with the "x" spec — cleaner and lets you pad.

format(255, "x")     # "ff"
format(255, "04x")   # "00ff"
f"{255:x}"           # "ff"

History

1.0
hex() has been a builtin since Python 1.0.
3.0
Long integers unified with int — no more "L" suffix.