hex() Integer to hex string
Last updated: 2026-09-22
hex() Integer to hex string
hex() converts an integer to a hex string prefixed with '0x'.
| Category | Built-in Functions |
|---|---|
| Kind | Built-in Function |
| Python Version | all |
📝 Syntax
hex(x)
⚙️ Parameters
| x Required | An integer (int or an object implementing __index__). |
|---|
Returns:Returns a lowercase hex string prefixed with '0x'.
💥 Raises
TypeError— Raised when the argument is not an integer.
▶ Example
Edit the code, press Run, and see the result in the output panel:
Output:
0xff
-0x2a
🏷️ Related Built-in Functions
💡 Related Cases
More case studies coming soon — check back later.
❓ FAQ
QWhat does hex(255) return?
AIt returns '0xff' — with the 0x prefix and lowercase letters; for uppercase use format(255, '#X').
QWhat is the difference between hex() and format(255, 'x')?
Ahex() includes the 0x prefix by itself; format(255, 'x') returns 'ff' with no prefix, and format(255, '#x') adds it — the format versions slot more easily into larger format strings.
QWhat does hex(-42) return?
AIt returns '-0x2a' — the minus sign comes before the 0x prefix.
QCan hex() convert a float?
ANo. It only accepts integers (or objects implementing __index__); passing a float raises a TypeError.