Dynamic Zero Padding.

Friedrich Clausen fred at derf.nl
Wed Jun 8 03:00:32 EDT 2011


On Wed, Jun 8, 2011 at 4:56 AM, Terry Reedy <tjreedy at udel.edu> wrote:
> 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'

Thanks all for the many good suggestions - I am looking over them and
reading the referenced docs.

Cheers,

Fred.



More information about the Python-list mailing list