How can I get Python to use upper case letters when printing hexadecimal values?

WilliamKF picture WilliamKF · Nov 7, 2012 · Viewed 29.9k times · Source

In Python v2.6 I can get hexadecimal for my integers in one of two ways:

print(("0x%x")%value)
print(hex(value))

However, in both cases, the hexadecimal digits are lower case. How can I get these in upper case?

Answer

Eric picture Eric · Nov 7, 2012

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}")