Shouldn't %06s zero-pad a string?
Beat Bolli
b11 at gmx.no-spam.net
Fri Feb 14 03:58:29 EST 2003
Jp Calderone wrote:
> On Thu, Feb 13, 2003 at 11:09:02PM +0100, Beat Bolli wrote:
>
>>Jeff Epler wrote:
>>
>>>Python generally takes inspiration for the handling of %-formats from
>>>the C standard.
>>>
>>>[Tests snipped]
>>>
>>>Here's what my printf manual page says about '0':
>>>
>>> 0 The value should be zero padded. For d, i, o, u,
>>> x, X, a, A, e, E, f, F, g, and G conversions, the
>>> converted value is padded on the left with zeros
>>> rather than blanks. If the 0 and - flags both
>>> appear, the 0 flag is ignored. If a precision is
>>> given with a numeric conversion (d, i, o, u, x, and
>>> X), the 0 flag is ignored. For other conversions,
>>> the behavior is undefined.
>>>
>>>so "the behavior is undefined" when the s conversion is used.
>>>The treatment of 0 as " " (space) seems fairly useful, though.
>>>
>>
>>That's all fine, but the *Python* manual mentions no such limitation.
>>Wouldn't be simpler to handle all cases consistently?
>
>
> Simpler for whom? (Answer this one in your head, please)
Well, for the guy who programs the % function, for one. I'm pretty sure
that the present behaviour is a special case (too lazy to check the
sources ATM !-)
On the other hand, for the user of the % function (principle of least
surprise...).
>
> If all you want is a string padded with zeros, use str.zfill().
Cool. the last time I checked, this wasn't a string method yet!
Beat
More information about the Python-list
mailing list