Python – Convert Decimal to Hex


Convert Decimal to Hex

To convert a given decimal number to hexadecimal number, call the builtin function hex() and pass the decimal number as argument to it. hex() converts the given decimal to hex, and returns as a string.


In this example, we write a function to read a decimal number from user, convert it to hex, and print the result to standard output.

Python Program

d = int(input('Enter decimal : '))
h = hex(d)
print('Hexadecimal :', h)

Output #1

Enter decimal : 12365
Hexadecimal : 0x304d

Output #2

Enter decimal : 12
Hexadecimal : 0xc


In this Python Examples tutorial, we learned how to convert decimal number to hexadecimal number using hex() builtin function.

Related Tutorials

Code copied to clipboard successfully 👍