How can I get Python to use upper case letters when printing hexadecimal values?
Capital X (Python 2 and 3 using sprintf-style formatting):
print("0x%X" % value)
Or in python 3+ (using .format
string syntax):
print("0x{:X}".format(value))
Or in python 3.6+ (using formatted string literals):
print(f"0x{value:X}")
Just use upper().
intNum = 1234hexNum = hex(intNum).upper()print('Upper hexadecimal number = ', hexNum)
Output:
Upper hexadecimal number = 0X4D2
By using uppercase %X
:
>>> print("%X" % 255)FF
Updating for Python 3.6 era: Just use 'X' in the format part, inside f-strings:
print(f"{255:X}")
(f-strings accept any valid Python expression before the :
- including direct numeric expressions and variable names).