3

I read here that The plan is to eventually make this ["".format()] the only API for string formatting, and to start deprecating the % operator in Python 3.1.

I tried the % syntax with Python 3.1, 3.2 and 3.3, and it's working. So is there still a plan to remove the % syntax from Python in a future version, or can I use it freely?

  • I've been tapped on my fingers by core devs by calling `%` deprecated here on Stack Overflow. It looks like it is sticking around for the 3.x series at least. – Martijn Pieters Apr 24 '13 at 15:29
  • not putting it as an answer as I dont have a source to cite ... but Im pretty sure they changed their mind and it will always(meaning the forseeable future) be there based on feedback from the community – Joran Beasley Apr 24 '13 at 15:29
  • 1
    Note that everything you can do with `%` you can do with `.format()` too, but the latter supports the `.__format__()` special method and is thus more extensible and flexible. Something to consider. :-) – Martijn Pieters Apr 24 '13 at 15:31
  • Not a full answer, but I [touched on the subject](http://stackoverflow.com/questions/13451989/pythons-many-ways-of-string-formatting-are-the-older-ones-going-to-be-deprec/13452357#13452357) before. – Martijn Pieters Apr 24 '13 at 15:31
  • I am marking this as a duplicate anyway, because there is another answer on that other question that expands on the position from the python-dev list: controversy over removing it, it'll stay in as people like it to stay. – Martijn Pieters Apr 24 '13 at 15:33
  • @MartijnPieters, I know *format* is sometimes better, but I have working code, and I wonder if it will be still working in futures versions. The % syntax is easier to type (to my taste at least), and it's maybe better to use a uniform syntax across modules, so I'd like to be sure. Of course, changing everything to *format* is a possibility. –  Apr 24 '13 at 15:37
  • Last but not least: A comment from [Ned Batchelder](http://stackoverflow.com/questions/14041791/print-variable-and-a-string-in-python/14041800#comment19400308_14041800) tapping me on the fingers for calling `%` semi-deprecated. – Martijn Pieters Apr 24 '13 at 15:38
  • Ok, given your answers and the usual python [philosophy](http://www.python.org/dev/peps/pep-0020/) that there should not be many ways to do one thing, I think it's safer to switch completely to *format*. Sorry for the duplicate question, and thank you anyway. –  Apr 24 '13 at 15:41
  • @MartijnPieters Given that there are no *definite* plans to deprecate the old syntax, it's probably a good idea to remove the assertion "The latter has been deprecated already" from that answer. – Aya Apr 24 '13 at 16:22
  • @Aya: The problem is that the *Python documentation* is still ambiguous about the deprecation. It certainly was named 'depracated' in the PEP and the 3.0 and 3.1 documntation, and the tutorials still warn you against it. I wish Guido would put his foot down on the issue and state something one way or the other. – Martijn Pieters Apr 24 '13 at 16:25
  • @MartijnPieters Ah. The classic "if in doubt, blame GvR" approach. ;-) The docs seem to just use the word 'preferred' which is not really the same thing, and given the ambiguity, an assertion like "The latter has been deprecated already" is, at the very least, a little misleading, given that the term "deprecated" is more commonly used to describe functions which emit a `DeprecationWarning`. – Aya Apr 24 '13 at 16:31
  • @Aya: the PEP introducing the new syntax named the old syntax as deprecated, and so did the documentation; I was going by those statements at the time of the answer. – Martijn Pieters Apr 24 '13 at 16:32
  • @MartijnPieters All I see in the PEP is the sentence "...both systems can co-exist until it comes time to deprecate the older system.", but makes no mention of when that might be, if ever. Anyway, no biggie, just thought it might be worth updating the answer, particularly if there have been later rumblings in python-dev which might contradict it. – Aya Apr 24 '13 at 16:41

1 Answers1

5

This is not a definitive answer, but it is too large to make the point in a comment. The change in documentation wording in subsequent versions definitely moves away from stating the % syntax is deprecated.

From Old String Formatting Operations in v3.0 and Old String Formatting Operations in v3.1:

The formatting operations described here are obsolete and may go away in future versions of Python.

From Old String Formatting Operations in v3.2:

However, there are no current plans to deprecate printf-style formatting.

Old String Formatting Operations in v3.3 makes no mention of deprecation plans.

This is not quite certain enough for me to consider it actionable, however, and it would be nice to find a source with a clear statement.

David Alber
  • 17,624
  • 6
  • 65
  • 71
  • All that said, however, the tutorials from each of these versions (here's the relevant section from [v3.1](http://docs.python.org/3.1/tutorial/inputoutput.html#old-string-formatting)) continue to say: "However, because this old style of formatting will eventually be removed from the language..." – David Alber Apr 24 '13 at 15:49
  • So the documentation is a bit self-contradictory. It would be nice to have a clear statement on this from Guido. –  Apr 24 '13 at 16:30