How can I get Python to use upper case letters when printing hexadecimal values?
38,037
Solution 1
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}")
Solution 2
Just use upper().
intNum = 1234
hexNum = hex(intNum).upper()
print('Upper hexadecimal number = ', hexNum)
Output:
Upper hexadecimal number = 0X4D2
Solution 3
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).
Solution 4
print(hex(value).upper().replace('X', 'x'))
Handles negative numbers correctly.
Solution 5
The more Python 3 idiom using f-strings would be:
value = 1234
print(f'0x{value:X}')
'0x4D2'
Notes (and why this is not a duplicate):
- shows how to avoid capitalizing the '0x' prefix, which was an issue in other answers
- shows how to get variable interpolation
f'{value}'
; nobody actually ever puts (hardcoded) hex literals in real code. There are plenty of pitfalls in doing variable interpolation: it's notf'{x:value}'
norf'{0x:value}'
norf'{value:0x}'
nor evenf'{value:%x}'
as I also tried. So many ways to trip up. It still took me 15 minutes of trial-and-error after rereading four tutorials and whatsnew docs to get the syntax. This answer shows how to get f-string variable interpolation right; others don't.
Author by
WilliamKF
Updated on December 08, 2020Comments
-
WilliamKF over 3 years
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?
-
Peter Mortensen almost 6 yearsBut I don't think "0X4D2" is the correct notation. It should be "0x4D2" (lowercase "x").
-
smci over 5 years'%X' is best. @PeterMortensen is correct. This way, you'd need something clunky like
''.join([ '0x', hex(value).upper()[2:] ])
-
smci over 5 yearsSee my answer for the more Python 3 idiom using f-strings:
print(f'0x{value:X}')
-
smci over 5 years@wim: No it's not a) jsbueno's answer show neither how to avoid capitalizing the '0x' prefix, nor b) how to get variable interpolation
f'{value}'
; nobody actually puts hex literals in real code. And there are pitfalls in doing interpolation: it's notf'{x:value}'
norf'{0x:value}'
norf'{value:0x}'
. Many ways to trip up. I show how not to. -
smci over 5 yearsYes. I'm saying
%
is the older operator hangover from 2.x which while not officially deprecated, is discouraged. Python 3.6+ finally allows variable interpolation like we've wanted ever since 3.0 and.format()
. -
Eric over 5 yearsIt's not much of a "Python 3 idiom" if it fails on 6/8 of all released versions of python 3 (although admittedly most are EOL)
-
smci over 5 yearsyes it is, and you're missing the point. This answer is an old near-deprecated 2.x idiom that is only still kept alive in 3.x because they messed up the transition to
format()
back in 3.0. As of today, "3.x" means "3.6+" or "3.7", where f-strings are awesome. 3.6 was a huge advance for string formatting. People today should be learning f-strings.%
-formatting-operator was planned to be deprecated but they got pushback because it's in a lot of legacy code. -
Eric over 5 years'As of today, "3.x" means "3.6+" or "3.7"' - in my experience, this is not true. For me, 3.x means "the earliest version of python 3 that my package will support", which typically is "all non-EOL python 3.x". "where f-strings are awesome" - you're not wrong about that.
-
Sean Burton almost 4 yearsAll of these give the wrong result if your value is negative. You get
0x-FF
when what you want is-0xFF
. -
Nathan Garabedian over 3 yearsAlthough it seems a little more clunky, this answer is the one that addresses negative numbers and also does not leave X uppercase, but adheres to the standard of x being lowercase. +1