Dynamic Zero Padding.

Terry Reedy tjreedy at udel.edu
Tue Jun 7 22:56:42 EDT 2011


On 6/7/2011 7:05 PM, John Posner wrote:

> 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'

One cannot use a nested field in the 'name' part of the field (before 
the ':' (I tried), but are pretty free to nest in the actual 
specification part after the ':'.

 >>> '{0:{1}}'.format(7,'b')
'111'
 >>> '{0:{1}}'.format(7,'d')
'7'

-- 
Terry Jan Reedy




More information about the Python-list mailing list