Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

python
python3
python programming
communitycreator

What is the hex() function in Python?

Maria Elijah

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

The hex() function

In Python, the hex() function gets the hexadecimal string of a given integer value.

Syntax

hex(number)

Parameter

  • number: This represents the integer value that needs to be converted to a hex string.

Note: The returned hex string always starts with the prefix 0x.

Code

The following code shows how to use the hex() function in Python:

# create an integer variable num
num = 45
# assign the hex value to the variable hex_num
hex_num = hex(num)
print(f"The hexadecimal string for {num}: {hex_num}")

Explanation

  • Line 2: We create a variable named num.

  • Line 4: We pass the value of num to the hex() function. Then, we assign the result to a new variable, hex_num.

  • Line 5: The result is displayed.

RELATED TAGS

python
python3
python programming
communitycreator

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring