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

Carsten Haese carsten.haese at gmail.com
Wed Dec 2 13:34:11 EST 2009


John Posner wrote:
>  Goal: place integer 456 flush-right in a field of width 8
> 
>   Py2: "%%%dd" % 8 % 456
>   Py3: "{0:{1}d}".format(456, 8)
> 
> With str.format(), you don't need to nest one formatting operation
> within another.

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

--
Carsten Haese
http://informixdb.sourceforge.net




More information about the Python-list mailing list