Built-in Functions and Types

hex()

Format an integer as base-16 text with the prefix 0x. Negative inputs place the minus sign before the prefix rather than emitting a fixed-width two’s-complement bit pattern. Objects implementing __index__() are accepted; floats are not implicitly truncated. Use format() if a prefix-free or width-controlled representation is needed.

Formal Syntax & Structure

python syntaxpython
hex(integer, /)

Parameters & Attributes

NameTypeDescription
integerint or object supporting __index__Integer-like value; positional only.

Return Value

str

A base-16 representation with 0x and an optional leading minus sign.

Working Example

Example Snippetpython
print(hex(48879))print(hex(-31))
Python Reference