[Python-Dev] transitioning from % to {} formatting

Eric Smith eric at trueblade.com
Wed Oct 7 20:21:01 CEST 2009

Antoine Pitrou wrote:
> Vinay Sajip <vinay_sajip <at> yahoo.co.uk> writes:
>>>>> "%0#8x" % 0x1234
>> '0x001234'
>>>>> "{0:0>#8x}".format(0x1234)
>> '000x1234'
> Apart from the sheer unreadability of the {}-style format string, the result 
> looks rather unexpected from a human being's point of view.
> (in those situations, I would output the 0x manually anyway, such as:
>>>> "0x%06x" % 0x1234
> '0x001234'
>>>> "0x{:06x}".format(0x1234)
> '0x001234'
> )

"#" formatting was added to int.__format__ in order to support this case:

 >>> format(10, '#6x')
'   0xa'

Without '#', there's no way to specify a field width but still have the 
'0x' up against the digits (at least not without generating an 
intermediate result and figuring out the width manually).

The fact that it works in combination with '0' or '>' (not sure which 
one makes it unreadable to you) wasn't really the point of the feature.


More information about the Python-Dev mailing list