[Tutor] Leading zero for hex numbers

Tony Cappellini tony at tcapp.com
Thu Dec 16 02:47:58 CET 2004


I'm trying to get Python to automatically print a leading 0 for hex
numbers, but it only
seems to work for for decimal numbers.

print "0x%0X" % 12345

displays
0x3039

instead of 0x03039


The Python docs state
The conversion will be zero padded for numeric values, when a 0 is used as
a flag between the % and the conversion type.

Is this expected Python behaviour, or a mistake?

Sure, I can add some code to calculate the length of the current string to
decide if a leading 0 is needed., but if I don't have to, I'd rather not.




More information about the Tutor mailing list