Moving from Python 2 to Python 3: A 4 page "cheat sheet"
Mark Dickinson
dickinsm at gmail.com
Wed Dec 2 13:32:56 EST 2009
On Dec 2, 4:41 pm, "John Posner" <jjpos... at optimum.net> 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. A little less mind-bending, and every little bit helps!
Or even "{:{}d}".format(456, 8), in 3.1 and 2.7 (when it appears).
But you can do this with % formatting, too. In either 2.x or 3.x:
>>> "%*d" % (8, 456)
' 456'
--
Mark
More information about the Python-list
mailing list