[FRPythoneers] dynamically created format strings?

Sean Reifschneider jafo at tummy.com
Mon Jan 26 11:38:30 MST 2004


On Mon, Jan 26, 2004 at 09:22:40AM -0700, Brian Jarrett wrote:
>iRight now I have a separate function created for each name and
>just pass it the data.  Also, I have do deal with alot of numbers,
>so I use format strings in separate functions like "%02d" and "%010d"
>to zero-pad numbers.  I'd like to just to be able to pass the number in
>and an integer representing how many digits should be used.

If I understand you correctly:

   >>> def foo(size, value): return(('%%0%dd' % size) % value)
   ... 
   >>> print foo(10, 1)
   0000000001
   >>> print foo(5, 10)
   00010

%% gets replaced by % in format string expansion, so the "'%%0%dd' % 10"
changes into '%010d'.

Sean
-- 
 A "fuddish" is when you *REALLY* like Looney Toons.
Sean Reifschneider, Member of Technical Staff <jafo at tummy.com>
tummy.com, ltd. - Linux Consulting since 1995.  Qmail, Python, SysAdmin
      Back off man. I'm a scientist.   http://HackingSociety.org/



More information about the FRPythoneers mailing list