Introduction: Python hex() Function
Sometimes you need to view an integer in hexadecimal form. This is common while working with memory addresses, color codes, bitwise operations, or low-level programming concepts.
Converting integers into hexadecimal manually takes extra work. The Python hex() Function converts an integer into hexadecimal with a single function call.
What it is: The hex() function is a built-in Python function that converts an integer into its hexadecimal representation. It returns the result as a string that begins with the 0x prefix, which indicates hexadecimal notation.
See a quick example to understand how the Python hex() Function works.
You can also explore its real-world use cases to see where this function is commonly used.
Before exploring practical examples and use cases, first learn its syntax, parameters and return value.
💡 Tip: Use the hex() function whenever you need the hexadecimal representation of an integer. To explore more built-in functions, visit the
Python Built-in Functions Learning Guide.
Syntax, Parameters, Return Value and Examples: Python hex() Function
The section below covers the syntax, parameters, return value, and a quick example of the Python hex() Function.
Syntax
hex(number)
Parameters
| Parameter | Description |
|---|---|
number |
The integer that will be converted into hexadecimal. Objects that implement the __index__() method are also accepted. |
Return Value
| Return Value | Description |
|---|---|
str |
Returns the hexadecimal representation of the given integer as a string with the 0x prefix. |
Quick Example
The following example converts a decimal number into hexadecimal.
number = 255
result = hex(number)
print(result)
# Output:
0xff
The hex() function converts the integer 255 into hexadecimal and returns the result as a string. The 0x prefix shows that the value is written in hexadecimal format.
How the Python hex() Function Works
- The
hex()function accepts an integer as input. - Python converts the integer into its hexadecimal representation.
- The converted value is returned as a string.
- The returned string always begins with the
0xprefix. - Negative integers keep the minus sign before the hexadecimal value.
- The original integer remains unchanged after the conversion.
Examples: Python hex() Function
The examples below show different ways to use the Python hex() Function.
Example 1: Convert a Decimal Number into Hexadecimal
number = 255
print(hex(number))
# Output:
0xff
Explanation: The hex() function converts the decimal value 255 into its hexadecimal representation. The returned string starts with 0x, which identifies it as a hexadecimal value.
Example 2: Convert Zero into Hexadecimal
print(hex(0))
# Output:
0x0
Explanation: Zero is also converted into hexadecimal. Python represents it as 0x0 using the same format as other integers.
Example 3: Convert a Negative Integer
print(hex(-42))
# Output:
-0x2a
Explanation: The hex() function also supports negative integers. Python places the minus sign before the hexadecimal representation.
Example 4: Remove the 0x Prefix
number = 100
hexa = hex(number)[2:]
print(hexa)
# Output:
64
Explanation: The value returned by hex() is a string. String slicing removes the 0x prefix and keeps only the hexadecimal digits.
Example 5: Convert User Input into Hexadecimal
number = int(input("Enter a number: "))
print(hex(number))
# Sample Output:
Enter a number: 128
0x80
Explanation: The input() function returns a string, so int() converts it into an integer first. The hex() function then converts that integer into hexadecimal.
Example 6: Convert Multiple Numbers into Hexadecimal
numbers = [10, 31, 64, 255]
for number in numbers:
print(number, "->", hex(number))
# Output:
10 -> 0xa
31 -> 0x1f
64 -> 0x40
255 -> 0xff
Explanation: The hex() function can be used inside a loop to convert several integers one after another. Each number is converted into its own hexadecimal representation.
Example 7: Handle an Invalid Argument
try:
print(hex(9.5))
except TypeError:
print("hex() requires an integer value")
# Output:
hex() requires an integer value
Explanation: The hex() function accepts only integers or objects that implement the __index__() method. Passing a floating-point value raises a TypeError.
Use Cases: When to use the hex() Function
The Python hex() Function is useful in situations like these:
- Converting decimal numbers into hexadecimal format.
- Working with hexadecimal color codes.
- Displaying memory addresses or hexadecimal values.
- Understanding bitwise operations.
- Checking hexadecimal values during debugging.
- Learning hexadecimal number systems.
Key Takeaways: hex() Function
Here are the main points to remember about the Python hex() Function:
- The
hex()function converts an integer into hexadecimal. - It returns the result as a string.
- The returned string always begins with the
0xprefix. - It supports both positive and negative integers.
- It also accepts objects that implement the
__index__()method. - Passing an unsupported data type raises a
TypeError.
The hex() function provides a simple way to convert integers into hexadecimal without writing your own conversion logic. It is useful for learning hexadecimal numbers, working with color codes, and checking hexadecimal values during debugging.