w3resource logo
Python Exercises

Python: Convert decimal to hexadecimal

Python Basic: Exercise-141 with Solution

Write a python program to convert decimal to hexadecimal.

Pictorial Presentation:

Convert decimal to hexadecimal

Sample Solution:-

Python Code:

x = 30
print(format(x, '02x'))
x = 4
print(format(x, '02x'))

Sample Output:


Visualize Python code execution:

The following tool visualize what the computer is doing step-by-step as it executes the said program:

Python Code Editor:


Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Write a Python program to convert an integer to binary keep leading zeros.
Next: Write a Python program to find the operating system name, platform and platform release date.