Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #7192 > unrolled thread

Re: Dynamic Zero Padding.

Started byEmile van Sebille <emile@fenx.com>
First post2011-06-07 14:50 -0700
Last post2011-06-07 14:50 -0700
Articles 1 — 1 participant

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: Dynamic Zero Padding. Emile van Sebille <emile@fenx.com> - 2011-06-07 14:50 -0700

#7192 — Re: Dynamic Zero Padding.

FromEmile van Sebille <emile@fenx.com>
Date2011-06-07 14:50 -0700
SubjectRe: Dynamic Zero Padding.
Message-ID<mailman.4.1307483354.11593.python-list@python.org>
On 6/7/2011 2:36 PM Friedrich Clausen said...
> Hello All,
>
> I want to print some integers in a zero padded fashion, eg. :
>
>>>> print("Testing %04i" % 1)
> Testing 0001
>
> but the padding needs to be dynamic eg. sometimes %05i, %02i or some
> other padding amount. But I can't insert a variable into the format
> specification to achieve the desirable padding.
>
> I would be much obliged if someone can give me some tips on how to
> achieve a variably pad a number.


Something like this works:

 >>> for ii in ((3,12),(4,140),(5,123)):
...   print ("Testing %%%02ii" % ii[0]) % ii[1]
...
Testing 012
Testing 0140
Testing 00123

Emile

[toc] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web