26

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?

smci
  • 32,567
  • 20
  • 113
  • 146
WilliamKF
  • 41,123
  • 68
  • 193
  • 295

5 Answers5

41

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}")
Flimm
  • 136,138
  • 45
  • 251
  • 267
Eric
  • 95,302
  • 53
  • 242
  • 374
  • 2
    See my answer for the more Python 3 idiom using f-strings: `print(f'0x{value:X}')` – smci Sep 06 '18 at 22:59
  • Yes. 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](https://docs.python.org/3.7/reference/lexical_analysis.html#index-20) like we've wanted ever since 3.0 and `.format()`. – smci Sep 07 '18 at 00:57
  • It'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) – Eric Sep 07 '18 at 01:13
  • yes 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](https://docs.python.org/3.7/reference/lexical_analysis.html#index-20). 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. – smci Sep 07 '18 at 01:18
  • 1
    _'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. – Eric Sep 07 '18 at 03:11
  • 1
    All of these give the wrong result if your value is negative. You get `0x-FF` when what you want is `-0xFF`. – Sean Burton Jun 18 '20 at 10:53
7

print(hex(value).upper().replace('X', 'x'))

Handles negative numbers correctly.

Simon Lindholm
  • 2,266
  • 1
  • 21
  • 12
  • Although 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 – Nathan Garabedian Nov 26 '20 at 13:18
6

Just use upper().

intNum = 1234
hexNum = hex(intNum).upper()
print('Upper hexadecimal number = ', hexNum)

Output:

Upper hexadecimal number =  0X4D2
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
nimesh
  • 113
  • 1
  • 2
  • 6
    But I don't think "0X4D2" is the correct notation. It should be "0x4D2" (lowercase "x"). – Peter Mortensen Jul 03 '18 at 02:11
  • 2
    '%X' is best. @PeterMortensen is correct. This way, you'd need something clunky like `''.join([ '0x', hex(value).upper()[2:] ])` – smci Sep 06 '18 at 22:00
5

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).

jsbueno
  • 99,910
  • 10
  • 151
  • 209
3

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 not f'{x:value}' nor f'{0x:value}' nor f'{value:0x}' nor even f'{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.
smci
  • 32,567
  • 20
  • 113
  • 146
  • @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 not `f'{x:value}'` nor `f'{0x:value}'` nor `f'{value:0x}'`. Many ways to trip up. I show how not to. – smci Sep 06 '18 at 23:49