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


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

Dynamic Zero Padding.

Started byFriedrich Clausen <fred@derf.nl>
First post2011-06-07 23:36 +0200
Last post2011-06-07 16:55 -0500
Articles 6 — 5 participants

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


Contents

  Dynamic Zero Padding. Friedrich Clausen <fred@derf.nl> - 2011-06-07 23:36 +0200
    Re: Dynamic Zero Padding. Mel <mwilson@the-wire.com> - 2011-06-07 17:43 -0400
      Re: Dynamic Zero Padding. Chris Angelico <rosuav@gmail.com> - 2011-06-08 09:26 +1000
      Re: Dynamic Zero Padding. Terry Reedy <tjreedy@udel.edu> - 2011-06-07 22:56 -0400
      Re: Dynamic Zero Padding. Friedrich Clausen <fred@derf.nl> - 2011-06-08 09:00 +0200
    Re: Dynamic Zero Padding. harrismh777 <harrismh777@charter.net> - 2011-06-07 16:55 -0500

#7188 — Dynamic Zero Padding.

FromFriedrich Clausen <fred@derf.nl>
Date2011-06-07 23:36 +0200
SubjectDynamic Zero Padding.
Message-ID<mailman.1.1307482621.11593.python-list@python.org>
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.

Cheers,

Fred.

Thanks,

Fred.

[toc] | [next] | [standalone]


#7190

FromMel <mwilson@the-wire.com>
Date2011-06-07 17:43 -0400
Message-ID<ism61s$grp$1@speranza.aioe.org>
In reply to#7188
Friedrich Clausen wrote:
> 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.

:)

('%%0%dd' % (pads,)) % (n,)

Probably be good to wrap it in a function.  It looks kind of obscure as it 
is.

	Mel.

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


#7201

FromChris Angelico <rosuav@gmail.com>
Date2011-06-08 09:26 +1000
Message-ID<mailman.9.1307489204.11593.python-list@python.org>
In reply to#7190
On Wed, Jun 8, 2011 at 7:43 AM, Mel <mwilson@the-wire.com> wrote:
> :)
>
> ('%%0%dd' % (pads,)) % (n,)
>
> Probably be good to wrap it in a function.  It looks kind of obscure as it
> is.

Would get rather pretty (read: ugly and impossible to read) if you
wanted to put a literal percent sign in front of the number.

:)

Chris Angelico

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


#7209

FromTerry Reedy <tjreedy@udel.edu>
Date2011-06-07 22:56 -0400
Message-ID<mailman.13.1307501817.11593.python-list@python.org>
In reply to#7190
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'

-- 
Terry Jan Reedy

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


#7215

FromFriedrich Clausen <fred@derf.nl>
Date2011-06-08 09:00 +0200
Message-ID<mailman.22.1307516436.11593.python-list@python.org>
In reply to#7190
On Wed, Jun 8, 2011 at 4:56 AM, Terry Reedy <tjreedy@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.

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


#7195

Fromharrismh777 <harrismh777@charter.net>
Date2011-06-07 16:55 -0500
Message-ID<F7xHp.1877$5v5.1386@newsfe11.iad>
In reply to#7188
Friedrich Clausen wrote:
> I would be much obliged if someone can give me some tips on how to
> achieve a variably pad a number.


>>>> b='04'
>>>> a="testing %"+b+"i"
>>>> print(a % 1)
> testing 0001




kind regards,
m harris

[toc] | [prev] | [standalone]


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


csiph-web