4

The Python 3.1.5 docs refer to the % style formatting as "Old String Formatting Operations" and provide this warning that it may be removed:

Note: The formatting operations described here are obsolete and may go away in future versions of Python. Use the new String Formatting in new code.

The replacement section appears to be printf-style String Formatting . It contains this warning:

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 str.format() interface helps avoid these errors, and also provides a generally more powerful, flexible and extensible approach to formatting text.

So it is still clearly discouraged, but why was the note about possibly removing the feature cut from the docs? Was this some sort of policy decision or enforcement, since it's not officially deprecated yet? Are there still plans to eventually deprecate and remove it?

4
  • What prevents you from simply using the new String Formatting operations? Oct 25, 2016 at 23:20
  • 1
    Note that 3.5 expanded the use of the % operator by adding it to the bytes and bytearray classes (PEP 461), so it seems like there are no plans to remove it any time soon.
    – dan04
    Oct 25, 2016 at 23:32
  • 1
    See also bugs.python.org/issue25407 Oct 25, 2016 at 23:32
  • 1
    @RobertHarvey Nothing. I prefer it. I'm wondering why the discouragement was relaxed. My question was prompted by someone suggesting that it's not discouraged, so I went looking for proof. I found a link to the 3.1.5 version here and tried to find the newer equivalent.
    – jpmc26
    Oct 25, 2016 at 23:34

1 Answer 1

3

Despite % deprecation, a huge amount of code, including for Python 3, still uses % instead of format(). Why? Partially because of the old habits, and partially because developers find that % is perfectly fine for their needs—frankly, as soon as it is used like this:

"Available quantity: %s." % (123,)

instead of:

"Available quantity: %s." % 123

it works pretty well: it may be ugly according to some people, but not any more risky than format().

As soon as a feature is broadly used, it can be depreciated, but can hardly be removed, at least from Python 2 and Python 3. Maybe Python 4 won't have it. Who knows.

Does it mean that it will always be available in Python 2 and Python 3? Maybe yes. Maybe no. It might happen that the usage of % would drastically drop in a few years, meaning that it could be removed without causing too much trouble.

Should you continue using it? No. format() is a superior alternative, so there are no reasons to stick with % in new codebases. If you're contributing to an old codebase which consistently uses %, stick with it: uniformity matters.

3
  • 2
    I disagree with the last sentence. I'm working on a codebase that used %, but the other developers and I have all started using format(), and replacing the old-style with the new-style when it's convenient. I think making a gradual change like this is fine.
    – gardenhead
    Oct 26, 2016 at 1:22
  • 1
    @gardenhead: exactly! I was talking about a case where you're a new contributor, for instance in an open source project. But if the whole team can decide that it's time to move to format(), then do it, absolutely. Oct 26, 2016 at 10:26
  • For comparison, the last time Python made a backwards-incompatible change to such a popularly-used operator (/ on ints), it was phased in over a period of 7 years (from __future__ import division in 2.2 (2001) to forcing the new behavior in 3.0 (2008)).
    – dan04
    Oct 28, 2016 at 22:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.