Search code examples
pythonformattinghexuppercase

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


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?


Solution

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