Moving from Python 2 to Python 3: A 4 page "cheat sheet"

John Posner jjposner at optimum.net
Wed Dec 2 17:49:43 EST 2009


On Wed, 02 Dec 2009 13:34:11 -0500, Carsten Haese  
<carsten.haese at gmail.com> wrote:

>
> With string interpolation, you don't need to do that, either.
>>>> '%*d' % (8,456)
> '     456'
>

Thanks, Carsten and Mark D. -- I'd forgotten about the use of "*" in  
minimum-field-width specs and precision specs (doh). How about this:

   "pi={1:.{0}f} e={2:.{0}f}".format(5, math.pi, math.e)

   (result: 'pi=3.14159 e=2.71828')

Can the Python2 %-formating facility handle this without repeating the "5"  
argument?

Even if it can, I stand by my original suggestion: include an example to  
show that the arguments to str.format() can be used on both the left and  
the right of a ":" in a replacement field.

-John



More information about the Python-list mailing list