Inconsistency in hex()

Bengt Richter bokr at
Wed Jul 13 01:07:51 CEST 2005

On Tue, 12 Jul 2005 21:17:07 +1000, Steven D'Aprano <steve at> wrote:

>hex() of an int appears to return lowercase hex digits, and hex() of a
>long uppercase.
>>>> hex(75)
>>>> hex(75*256**4)
>By accident or design? Apart from the aesthetic value that lowercase hex
>digits are ugly, should we care?
>It would also be nice if that trailing L would disappear.
 >>> '%010X'% 0x12345678
 >>> '%010X'% 75
 >>> '%010X'% (75*256**4)
 >>> '%010X'% (-75*256**4)
 >>> '%010X'% (-75)

I've ranted about the lack of a natural format for showing
the hex of a canonical twos-complement representation of a negative number,
but I guess I'll let it go with this mention ;-)

BTW, yeah, I know it's not so hard to write
 >>> '%010X'% (-75 &0xffffffffff)
 >>> '%010X'% (-75*256**4 &0xffffffffff)
or a helper or a str subclass that does __mod__ differently but that's not with the batteries ;-/
Bengt Richter

More information about the Python-list mailing list