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


Groups > comp.lang.python > #7193

Re: Dynamic Zero Padding.

Date 2011-06-07 15:04 -0700
From Ethan Furman <ethan@stoneleaf.us>
Subject Re: Dynamic Zero Padding.
References <BANLkTik3hcA2vVfs3jCnbcRdh3_rKhU3ow@mail.gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.5.1307483556.11593.python-list@python.org> (permalink)

Show all headers | View raw


Friedrich Clausen 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.

--> width = 3
--> print("Testing %0*i" % (width, 1))
Testing 001
--> width = 7
--> print("Testing %0*i" % (width, 1))
Testing 0000001

The '*' acts as a place holder for the width argument.

~Ethan~

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


Thread

Re: Dynamic Zero Padding. Ethan Furman <ethan@stoneleaf.us> - 2011-06-07 15:04 -0700
  Re: Dynamic Zero Padding. harrismh777 <harrismh777@charter.net> - 2011-06-07 17:01 -0500
    Re: Dynamic Zero Padding. Larry Hudson <orgnut@yahoo.com> - 2011-06-07 18:46 -0700

csiph-web