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


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

Re: Dynamic Zero Padding.

Started byEthan Furman <ethan@stoneleaf.us>
First post2011-06-07 15:04 -0700
Last post2011-06-07 18:46 -0700
Articles 3 — 3 participants

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

#7193 — Re: Dynamic Zero Padding.

FromEthan Furman <ethan@stoneleaf.us>
Date2011-06-07 15:04 -0700
SubjectRe: Dynamic Zero Padding.
Message-ID<mailman.5.1307483556.11593.python-list@python.org>
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~

[toc] | [next] | [standalone]


#7197

Fromharrismh777 <harrismh777@charter.net>
Date2011-06-07 17:01 -0500
Message-ID<RcxHp.6670$EU5.5710@newsfe17.iad>
In reply to#7193
Ethan Furman wrote:
> --> print("Testing %0*i" % (width, 1))

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

very nice...

[toc] | [prev] | [next] | [standalone]


#7204

FromLarry Hudson <orgnut@yahoo.com>
Date2011-06-07 18:46 -0700
Message-ID<ANadncITMO0USXPQnZ2dnUVZ5gmdnZ2d@giganews.com>
In reply to#7197
On 06/07/2011 03:01 PM, harrismh777 wrote:
> Ethan Furman wrote:
>> --> print("Testing %0*i" % (width, 1))
>
>> The '*' acts as a place holder for the width argument.
>
> very nice...
>

It works for precision as well as width.

wid = 10
prec = 3
num = 123.456789
print "%0*.*f" % (wid, prec, num)

gives you ->  000123.457

(It's the same as the printf() function in C.)

      -=- Larry -=-

[toc] | [prev] | [standalone]


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


csiph-web