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

Mark Summerfield list at qtrac.plus.com
Thu Dec 3 03:11:29 EST 2009


On 2 Dec, 22:49, "John Posner" <jjpos... at optimum.net> wrote:
> On Wed, 02 Dec 2009 13:34:11 -0500, Carsten Haese  
>
> <carsten.ha... 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

I can't squeeze another line into the last column of the last page (I
already tried).

However, a natural place to put this would be in the first section on
the first page. But I want _common_ not _clever_, i.e., an example
that shows a common use of * in % that I can then show using a nested
replacement field. The idea is that people can look at the left hand
column, recognize an idiom they use, and then be able to figure out
from the right hand column how to do the same in Python 3.

Anyway, I've come up with a tiny example and managed to squeeze it in,
so hopefully it'll appear on InformIT's web site in a day or so.



More information about the Python-list mailing list