Python Reference Library →

Python hex() Function – Convert Integer to Hexadecimal

Let’s look at hex() function – convert integer to hexadecimal

  1. Notice

    hex() returns a string with an integer’s hexadecimal representation, including a 0x prefix.

  2. Try

    Follow the example, change its sample, and compare what happens.

  3. Check

    Explain the result and test your understanding.

Watch how it works

Focus on how the names, objects, and calculations change. We’ll inspect the printed output in the next section.

Explore the idea

Follow the code and watch its state change. Guided simulation.

Code
Python
number = 15
print(hex(number))
Live state

Top-level scope

number→Object 1
Object 1 · int15
This step

hex() returns a string with an integer’s hexadecimal representation, including a 0x prefix.

Picture hex() function – convert integer to hexadecimal

hex() returns a string with an integer’s hexadecimal representation, including a 0x prefix.

Image brief

Alt text
A diagram for Python hex() Function – Convert Integer to Hexadecimal. Instructions: number = 15 print(hex(number)). Displayed result: 0xf. The final example bindings are number --> object 1 (int). object 1: 15
Image generation prompt
Create a clean educational illustration for a Python programming lesson titled "Python hex() Function – Convert Integer to Hexadecimal". Teaching focus: hex() returns a string with an integer’s hexadecimal representation, including a 0x prefix. Depict exactly the relationships and labels in this reference diagram: EXAMPLE INSTRUCTIONS number = 15 print(hex(number)) | v DISPLAYED RESULT 0xf FINAL BINDINGS number --> object 1 (int) object 1: 15 Use a flat vector style, generous spacing, clear arrows and legible labels, with a restrained blue/teal palette on a plain background. The diagram must explain the programming idea accurately. Do not depict variables as boxes containing copied objects when they share a reference. Avoid mascots, decorative screenshots, logos, gradients and extra text. Landscape composition, approximately 3:2, suitable for a course card. Accessible description: A diagram for Python hex() Function – Convert Integer to Hexadecimal. Instructions: number = 15 print(hex(number)). Displayed result: 0xf. The final example bindings are number --> object 1 (int). object 1: 15

Follow the execution

Choose a different sample input, then follow every step through to the output. Before revealing it, predict what the program will display.

Execution workspace

Choose a sample and follow its execution. Guided simulation.

Code
Python
number = 15
print(hex(number))
Live state

Top-level scope

number→Object 1
Object 1 · int15
This step

The example has finished. Compare the final output with the values and references above.

Terminal
Output
0xf

What do you expect this program to print?

What does this example display?

number = 15
print(hex(number))
What does this example display?
Choose one answer. You can try again.

Answer and explanation

0xf

Follow the printed values in order. The output is: 0xf

More examples and reference

Python hex() Function

The hex() function in Python is used to convert an integer number to a hexadecimal string (base 16), prefixed with 0x.

This is useful when working with colors, memory addresses, or binary data formats.

Syntax

hex(number)

Parameters:

  • number – A valid integer (positive or negative)

Returns:

  • A string starting with '0x' followed by the hexadecimal equivalent of the integer.

Example: Convert Positive Integer to Hex

print(hex(255))
0xff

Example: Convert Negative Integer to Hex

print(hex(-42))
-0x2a

Use Case: RGB Colors in Web Design

Each color component in an RGB color code is often represented in hexadecimal:

r = 255
g = 165
b = 0

hex_color = f"#{hex(r)[2:]}{hex(g)[2:]}{hex(b)[2:]}"
print(hex_color)
#ffa500

Note: [2:] is used to remove the '0x' prefix.

Common Mistakes

  • Passing a float: hex(3.14) will raise TypeError
  • Passing a string: hex("10") is invalid
  • Always pass an integer type: use int() if needed

Interview Tip

In interviews, hex() is often used in bit manipulation or encoding problems. Know how to combine it with int(), bin(), or ord().

Summary

  • hex() converts an integer to a base-16 (hexadecimal) string
  • Output starts with '0x' for positive or '-0x' for negative
  • Does not work on float or string types directly

Practice Problem

Write a program that reads an integer from the user and prints its hexadecimal equivalent.

num = int(input("Enter an integer: "))
print("Hexadecimal:", hex(num))

What to remember

Connect what you noticed in the example with the idea below.

Remember

hex() returns a string with an integer’s hexadecimal representation, including a 0x prefix.

Watch out for this idea

hex() modifies the integer into text in place.

This is the misconception to avoid.

Reference checks are optional and do not count toward course progress.

    Reference library →