Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder1.enfer-du-nord.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.004 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; '>>>>': 0.09; 'integers': 0.09; 'padding': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:80.91.229.12': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'received:lo.gmane.org': 0.09; '>>>': 0.12; 'eg.': 0.16; 'pad': 0.16; 'padded': 0.16; 'padding.': 0.16; 'insert': 0.19; 'header :In-Reply-To:1': 0.21; 'variable': 0.21; 'testing': 0.27; 'all,': 0.30; 'skip:( 20': 0.30; 'print': 0.31; "can't": 0.32; 'header:X -Complaints-To:1': 0.32; 'someone': 0.33; 'to:addr:python-list': 0.33; 'header:User-Agent:1': 0.35; 'something': 0.37; 'desirable': 0.37; 'tips': 0.37; 'received:org': 0.38; 'but': 0.38; 'subject:: ': 0.38; 'some': 0.38; 'sometimes': 0.39; 'header:Mime-Version:1': 0.39; 'to:addr:python.org': 0.39; 'format': 0.40; 'received:173': 0.60; '0140': 0.84; 'amount.': 0.84; 'fashion,': 0.84 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Emile van Sebille Subject: Re: Dynamic Zero Padding. Date: Tue, 07 Jun 2011 14:50:56 -0700 References: Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Gmane-NNTP-Posting-Host: 173-11-108-137-sfba.hfc.comcastbusiness.net User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.17) Gecko/20110414 Lightning/1.0b2 Thunderbird/3.1.10 In-Reply-To: X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 27 NNTP-Posting-Host: 82.94.164.166 X-Trace: 1307483355 news.xs4all.nl 49177 [::ffff:82.94.164.166]:39892 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:7192 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