[Tutor] A file containing a string of 1 billion random digits.

Steven D'Aprano steve at pearwood.info
Tue Jul 20 01:53:25 CEST 2010


On Tue, 20 Jul 2010 03:07:47 am Richard D. Moores wrote:
> On Mon, Jul 19, 2010 at 09:58, ALAN GAULD <alan.gauld at btinternet.com> 
wrote:
> > Heres what I did:
> > Search Google for "Python format strings" and from the first link
> > click on String Formatting operations in the contents pane:
> >
> > http://docs.python.org/library/stdtypes.html#string-formatting-oper
> >ations
> >
> > Read item number 4.
>
> "4. Minimum field width (optional). If specified as an '*'
> (asterisk), the actual width is read from the next element of the
> tuple in values, and the object to convert comes after the minimum
> field width and optional precision."
>
> Now that just screams for about a dozen well-designed illustrative
> examples, don't you think?

Surely it only needs one?

(a) Field width known when you write the template, compared to (b) it 
being unknown when you write the template:

>>> template = "(a) %05d | (b) %0*d"
>>> template % (42, 5, 42)
'(a) 00042 | (b) 00042'


This is how you would do it with the asterisk: you need a meta-template 
to make a template.

>>> meta = "(a) %%05d | (b) %%0%dd"
>>> template = meta % 5
>>> template % (42, 42)
'(a) 00042 | (b) 00042'




-- 
Steven D'Aprano


More information about the Tutor mailing list