Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Dynamic Zero Padding.

31 views
Skip to first unread message

Friedrich Clausen

unread,
Jun 7, 2011, 5:36:58 PM6/7/11
to pytho...@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.

Mel

unread,
Jun 7, 2011, 5:43:19 PM6/7/11
to

:)

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

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

Mel.

Chris Rebert

unread,
Jun 7, 2011, 5:44:58 PM6/7/11
to Friedrich Clausen, pytho...@python.org
On Tue, Jun 7, 2011 at 2:36 PM, Friedrich Clausen <fr...@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

Emile van Sebille

unread,
Jun 7, 2011, 5:50:56 PM6/7/11
to pytho...@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

Ethan Furman

unread,
Jun 7, 2011, 6:04:35 PM6/7/11
to Friedrich Clausen, pytho...@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~

harrismh777

unread,
Jun 7, 2011, 5:55:49 PM6/7/11
to
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

Prasad, Ramit

unread,
Jun 7, 2011, 5:54:41 PM6/7/11
to Friedrich Clausen, pytho...@python.org
Python 2.6.x
>>> 'test {0:.2f}'.format(1)
'test 1.00'
>>> 'test {0:{1}f}'.format(1,2)
'test 1.000000'
>>> 'test {0:{1}f}'.format(1,.2)
'test 1.00'
>>> 'test {0:.{1}f}'.format(1,2)
'test 1.00'


Ramit


Ramit Prasad | JPMorgan Chase Investment Bank | Currencies Technology
712 Main Street | Houston, TX 77002
work phone: 713 - 216 - 5423


-----Original Message-----
From: python-list-bounces+ramit.prasad=jpmcha...@python.org [mailto:python-list-bounces+ramit.prasad=jpmcha...@python.org] On Behalf Of Friedrich Clausen
Sent: Tuesday, June 07, 2011 4:37 PM
To: pytho...@python.org
Subject: Dynamic Zero Padding.

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.
--
http://mail.python.org/mailman/listinfo/python-list
This communication is for informational purposes only. It is not
intended as an offer or solicitation for the purchase or sale of
any financial instrument or as an official confirmation of any
transaction. All market prices, data and other information are not
warranted as to completeness or accuracy and are subject to change
without notice. Any comments or statements made herein do not
necessarily reflect those of JPMorgan Chase & Co., its subsidiaries
and affiliates.

This transmission may contain information that is privileged,
confidential, legally privileged, and/or exempt from disclosure
under applicable law. If you are not the intended recipient, you
are hereby notified that any disclosure, copying, distribution, or
use of the information contained herein (including any reliance
thereon) is STRICTLY PROHIBITED. Although this transmission and any
attachments are believed to be free of any virus or other defect
that might affect any computer system into which it is received and
opened, it is the responsibility of the recipient to ensure that it
is virus free and no responsibility is accepted by JPMorgan Chase &
Co., its subsidiaries and affiliates, as applicable, for any loss
or damage arising in any way from its use. If you received this
transmission in error, please immediately contact the sender and
destroy the material in its entirety, whether in electronic or hard
copy format. Thank you.

Please refer to http://www.jpmorgan.com/pages/disclosures for
disclosures relating to European legal entities.

harrismh777

unread,
Jun 7, 2011, 6:01:20 PM6/7/11
to
Ethan Furman wrote:
> --> print("Testing %0*i" % (width, 1))

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

very nice...

MRAB

unread,
Jun 7, 2011, 6:00:00 PM6/7/11
to pytho...@python.org
On 07/06/2011 22:36, 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.
>
>>> "Testing %0*i" % (4, 1)
'Testing 0001'
>>> "Testing %0*i" % (5, 1)
'Testing 00001'

Chris Angelico

unread,
Jun 7, 2011, 7:26:40 PM6/7/11
to pytho...@python.org
On Wed, Jun 8, 2011 at 7:43 AM, Mel <mwi...@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

Larry Hudson

unread,
Jun 7, 2011, 9:46:48 PM6/7/11
to

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

Terry Reedy

unread,
Jun 7, 2011, 10:56:42 PM6/7/11
to pytho...@python.org
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

Friedrich Clausen

unread,
Jun 8, 2011, 3:00:32 AM6/8/11
to Terry Reedy, pytho...@python.org

Thanks all for the many good suggestions - I am looking over them and
reading the referenced docs.

Cheers,

Fred.

0 new messages