New Python 3.0 string formatting - really necessary?

Steven D'Aprano steve at
Sat Dec 20 02:54:35 CET 2008

On Fri, 19 Dec 2008 10:25:30 -0700, Michael Torrie wrote:

> So funny that now that Python 3.0 is actually released we have people
> acting all surprised like they've never seen any of the new features in
> Python 3.0 coming.  However these features have been discussed for
> years!  And debated!

Debated by who? The entire Python-using community? Every single Python 
programmer? Or just the small proportion of Python developers who are 
also core developers?

Are you *really* surprised that some people had never heard of the 
changes being debated until it was too late?

> Personally the new string formatter is sorely needed in Python.  And
> they way it has been implemented is a thing of beauty.  Basically the
> burden of formatting strings has been moved from the print
> statement/function to the objects themselves.

That's clearly not true. The print statement was not involved in 
formatting strings in the past. From Python 2.5:

>>> s = "%s and %s" % ("spam", "eggs")
>>> list(s)
['s', 'p', 'a', 'm', ' ', 'a', 'n', 'd', ' ', 'e', 'g', 'g', 's']

No print required until well after the string substitution was completed.

> Furthermore, the new {}
> notation allows many, many more options for formatting to be used.  Want
> to display a floating point number with $#.## notation, and ($#.##) for
> negative?  It's all now possible.  Couldn't be done before.

Of course it could be. You just needed to write your own formatting 
engine. What you mean is that it couldn't be done with % formatting and 
nothing else. 

> In short, this is a huge improvement, and backwards compatibility is
> preserved for the 2.x style for those that wish it.

There clearly is a need for a more heavyweight formatting solution than % 
and string.Template. There are things that can't be done easily with % 
alone, and format() will make them much simpler. I have no objection to 
the addition of the format() method (although I wonder whether it might 
have been better as a function).


More information about the Python-list mailing list