Python's many ways of string formatting — are the older ones (going to be) deprecated? Python's many ways of string formatting — are the older ones (going to be) deprecated? python python

Python's many ways of string formatting — are the older ones (going to be) deprecated?


The new .format() method is meant to replace the old % formatting syntax. The latter has been de-emphasised, (but not officially deprecated yet). The method documentation states as much:

This method of string formatting is the new standard in Python 3, and should be preferred to the % formatting described in String Formatting Operations in new code.

(Emphasis mine).

To maintain backwards compatibility and to make transition easier, the old format has been left in place for now. From the original PEP 3101 proposal:

Backwards Compatibility

Backwards compatibility can be maintained by leaving the existing mechanisms in place. The new system does not collide with any of the method names of the existing string formatting techniques, so both systems can co-exist until it comes time to deprecate the older system.

Note the until it comes time to deprecate the older system; it hasn't been deprecated, but the new system is to be used whenever you write new code.

The new system has as an advantage that you can combine the tuple and dictionary approach of the old % formatter:

"{greeting}, {0}".format(world, greeting='Hello')

and is extensible through the object.__format__() hook used to handle formatting of individual values.

Note that the old system had % and the Template class, where the latter allows you to create subclasses that add or alter its behaviour. The new-style system has the Formatter class to fill the same niche.

Python 3 has further stepped away from deprecation, instead giving you warning in the printf-style String Formatting section:

Note: The formatting operations described here exhibit a variety of quirks that lead to a number of common errors (such as failing to display tuples and dictionaries correctly). Using the newer formatted string literals or the str.format() interface helps avoid these errors. These alternatives also provide more powerful, flexible and extensible approaches to formatting text.

Python 3.6 also added formatted string literals, which in-line the expressions into the format strings. These are the fastest method of creating strings with interpolated values, and should be used instead of str.format() wherever you can use a literal.


The % operator for string formatting is not deprecated, and is not going to be removed - despite the other answers.
Every time the subject is raised on Python development list, there is strong controversy on which is better, but no controversy on whether to remove the classic way - it will stay. Despite being denoted on PEP 3101, Python 3.1 had come and gone, and % formatting is still around.

The statements for the keeping classic style are clear: it is simple, it is fast, it is quick to do for short things. Using the .format method is not always more readable - and barely anyone - even among the core developers, can use the full syntax provided by .format without having to look at the reference Even back in 2009, one had messages like this: http://mail.python.org/pipermail/python-dev/2009-October/092529.html - the subject had barely showed up in the lists since.

2016 update

In current Python development version (which will become Python 3.6) there is a third method of string interpolation, described on PEP-0498. It defines a new quote prefix f"" (besides the current u"", b"" and r"").

Prefixing a string by f will call a method on the string object at runtime, which will automatically interpolate variables from the current scope into the string:

>>> value = 80>>> f'The value is {value}.''The value is 80.'


Guido's latest position on this seems to be indicated here:

What’s New In Python 3.0

PEP 3101: A New Approach To String Formatting

A new system for built-in string formatting operations replaces the % string formatting operator. (However, the % operator is still supported; it will be deprecated in Python 3.1 and removed from the language at some later time.) Read PEP 3101 for the full scoop.

And the PEP3101 itself, which has the last modified dating back to (Fri, 30 Sep 2011), so no progress as of late on that one, I suppose.