Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!.POSTED!not-for-mail From: Mel Newsgroups: comp.lang.python Subject: Re: Dynamic Zero Padding. Followup-To: comp.lang.python Date: Tue, 07 Jun 2011 17:43:19 -0400 Organization: Aioe.org NNTP Server Lines: 22 Message-ID: References: Reply-To: mwilson@the-wire.com NNTP-Posting-Host: p6WYW/4RfMNjw4I/OJM4Cg.user.speranza.aioe.org Mime-Version: 1.0 Content-Type: text/plain; charset="ISO-8859-1" Content-Transfer-Encoding: 7Bit X-Complaints-To: abuse@aioe.org User-Agent: KNode/4.4.8 X-Notice: Filtered by postfilter v. 0.8.2 Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:7190 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.