hex(dummy)[2:] - issue...
MRAB
google at mrabarnett.plus.com
Wed May 6 14:28:02 EDT 2009
Florian Wollenschein wrote:
> Hi there,
>
> I need some advice :-)
> I'm using hex(dummy)[2:] to represent a color in hexadecimal format for
> the bgcolor in an html file. dummy is the color value in RGB of course...
>
> Now, if there's an R, G or B value of zero, this command only prints one
> single 0 instead of two. What's wrong with the code?
>
hex() returns '0x' followed by no more digits than are necessary:
>>> hex(0xFF)
'0xff'
>>> hex(0xF)
'0xf'
Try "%02X" instead (it'll pad with leading zeros up to a width of 2):
>>> "%02X" % 0xFF
'FF'
>>> "%02X" % 0xF
'0F'
More information about the Python-list
mailing list