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


Groups > comp.lang.python > #7191

Re: Dynamic Zero Padding.

References <BANLkTik3hcA2vVfs3jCnbcRdh3_rKhU3ow@mail.gmail.com>
Date 2011-06-07 14:44 -0700
Subject Re: Dynamic Zero Padding.
From Chris Rebert <clp2@rebertia.com>
Newsgroups comp.lang.python
Message-ID <mailman.3.1307483101.11593.python-list@python.org> (permalink)

Show all headers | View raw


On Tue, Jun 7, 2011 at 2:36 PM, Friedrich Clausen <fred@derf.nl> wrote:
> 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.

http://docs.python.org/library/stdtypes.html#str.zfill

Cheers,
Chris

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: Dynamic Zero Padding. Chris Rebert <clp2@rebertia.com> - 2011-06-07 14:44 -0700

csiph-web