How to use hex() function In Python

hex() function is one of the built-in functions in Python3, which is used to convert an integer number into its corresponding hexadecimal form.

Syntax : hex(x) 

Parameters : 

  • x – an integer number (int object)

Returns :  Returns hexadecimal string.

Errors and Exceptions :

TypeError :  Returns TypeError when anything other than

             integer type constants are passed as parameters.   

Code :

Python3




# Python3 program to illustrate
# hex() function
  
print("The hexadecimal form of 69 is "
      + hex(69))


Output:

The hexadecimal form of 69 is 0x45

Python Program to Convert Decimal to Hexadecimal

In this article, we will learn how to convert a decimal value(base 10) to a hexadecimal value (base 16) in Python. 

Similar Reads

Method 1: Using hex() function

hex() function is one of the built-in functions in Python3, which is used to convert an integer number into its corresponding hexadecimal form....

Method 2: Iterative Approach

...

Method 3: Recursive Approach

The conventional method for converting decimal to hexadecimal is to divide it by 16 until it equals zero. The hexadecimal version of the given decimal number is the sequence of remainders from last to first in hexadecimal form. To convert remainders to hexadecimal form, use the following conversion table:...