Python's many ways of string formatting — are the older ones (going to be) deprecated?
backwards-compatibility, deprecated, printf, python, string-formatting
Solution
While there are various indications in the docs that `.format` and f-strings are superior to `%` strings, there's no surviving plan to ever deprecate the latter.
In commit Issue #14123: Explicitly mention that old style % string formatting has caveats but is not going away any time soon., inspired by issue Indicate that there are no current plans to deprecate printf-style formatting, the docs on `%`-formatting were edited to contain this phrase:
As the new string-formatting syntax is more flexible and handles tuples and dictionaries naturally, it is recommended for new code. However, there are no current plans to deprecate printf-style formatting.
(Emphasis mine.)
This phrase was removed later, in commit Close #4966: revamp the sequence docs in order to better explain the state of modern Python. This might seem like a sign that a plan to deprecate `%` formatting was back on the cards... but diving into the bug tracker reveals that the intent was the opposite. On the bug tracker, the author of the commit characterises the change like this:
- changed the prose that describes the relationship between printf-style formatting and the str.format method (deliberately removing the implication that the former is any real danger of disappearing - it's simply not practical for us to seriously contemplate killing it off)
In other words, we've had two consecutive changes to the `%`-formatting docs intended to explicitly emphasise that it will not be deprecated, let alone removed. The docs remain opinionated on the relative merits of different kinds of string formatting, but they're also clear the `%`-formatting isn't going to get deprecated or removed.
What's more, the most recent change to that paragraph, in March 2017, changed it from this...
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.
... to this:
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, the `str.format` interface, or template strings may help avoid these errors. Each of these alternatives provides their own trade-offs and benefits of simplicity, flexibility, and/or extensibility.
Notice the change from "helps avoid" to "may help avoid", and how the clear recommendation of `.format` and f-strings has been replaced by fluffy, equivocal prose about how each style "provides their own trade-offs and benefits". That is, not only is a formal deprecation no longer on the cards, but the current docs are openly acknowledging that `%` formatting at least has some "benefits" over the other approaches.
I'd infer from all this that the movement to deprecate or remove `%` formatting has not only faltered, but been defeated thoroughly and permanently.
Problem
Python has at least six ways of formatting a string: ``` In [1]: world = "Earth" # method 1a In [2]: "Hello, %s" % world Out[2]: 'Hello, Earth' # method 1b In [3]: "Hello, %(planet)s" % {"planet": world} Out[3]: 'Hello, Earth' # method 2a In [4]: "Hello, {0}".format(world) Out[4]: 'Hello, Earth' # method 2b In [5]: "Hello, {planet}".format(planet=world) Out[5]: 'Hello, Earth' # method 2c In [6]: f"Hello, {world}" Out[6]: 'Hello, Earth' In [7]: from string import Template # method 3 In [8]: Template("Hello, $planet").substitute(planet=world) Out[8]: 'Hello, Earth' ``` A brief history of the different methods: - `printf`-style formatting has been around since Pythons infancy - The `Template` class was introduced in Python 2.4 - The `format` method was introduced in Python 2.6 - `f`-strings were introduced in Python 3.6 My questions are: - Is `printf`-style formatting deprecated or going to be deprecated? - In the `Template class`, is the `substitute` method deprecated or going to be deprecated? (I'm not talking about `safe_substitute`, which as I understand it offers unique capabilities) Similar questions and why I think they're not duplicates: Python string formatting: % vs. .format — treats only methods 1 and 2, and asks which one is better; my question is explicitly about deprecation in the light of the Zen of Python String formatting options: pros and cons — treats only methods 1a and 1b in the question, 1 and 2 in the answer, and also nothing about deprecation advanced string formatting vs template strings — mostly about methods 1 and 3, and doesn't address deprecation String formatting expressions (Python) — answer mentions that the original '%' approach is planned to be deprecated. But what's the difference between planned to be deprecated, pending deprecation and actual deprecation? And the `printf`-style method doesn't raise even a `PendingDeprecationWarning`, so is this really going to be deprecated? This post is also quite old, so the information may be outdated. See also - PEP 502: String Interpolation - Extended Discussion - String Formatter