In this tutorial we will see how to convert a decimal value to hexadecimal using a built-in python function.
Program for decimal to hexadecimal conversion
The hex() function converts an integer number to a lowercase hexadecimal string prefixed with “0x”.
0x prefix represent hexadecimal
0b prefix is for binary
0o prefix is for octal
# Program published on https://beginnersbook.com
# Python program to convert decimal to hexadecimal
# Getting the decimal value from user
decimal = int(input("Enter the decimal value for conversion: "))
# converting using built-in function hex()
hex = hex(decimal)
print("The equivalent hexadecimal value is: ", hex)
Output:
Enter the decimal value for conversion: 679 The equivalent hexadecimal value is: 0x2a7

Reference:
Python docs – built-in functions hex()
Related Python Examples:
1. Python program to convert kilometers to Miles
2. Python program to swap two variables
3. Python program to convert decimal to binary
4. Python program to reverse a String using recursion
5. Python – Getting input from user
Leave a Reply