Dynamic Zero Padding.
John Posner
jjposner at codicesoftware.com
Tue Jun 7 19:05:47 EDT 2011
Friedrich:
<snip>
>> I would be much obliged if someone can give me some tips on how to
>> achieve a variably pad a number.
> :)
>
> ('%%0%dd' % (pads,)) % (n,)
>
> Probably be good to wrap it in a function. It looks kind of obscure as it
> is.
You might want to try "new style" string formatting [1], which I think
is better than the "old style" in this particular case:
>>> "Testing {0:0{1}d}".format(42, 4)
'Testing 0042'
>>> "Testing {0:0{1}d}".format(42, 9)
'Testing 000000042'
HTH,
John
[1] http://docs.python.org/library/string.html, Section 7.1.2 "String
Formatting"
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20110607/c136af91/attachment-0001.html>
More information about the Python-list
mailing list