I have finally switched from % to the .format() string formatting operator in my 2.x code in order to make it easier to migrate to 3.x in future. It was a bit surprising to find out that not only the %-style formatting remains in Py3, but it is widely used in the standard library code. It seems logical, because writing '(%s)' % variable is a bit shorter and maybe easier to comprehend than '({})'.format(variable). But I’m still in doubt. Is it proper (pythonic?) to use both approaches in the code?
Thank you.
I have finally switched from % to the .format() string formatting operator in my
Share
Python 3.2 documentation said that,
%will eventually go away.http://docs.python.org/3.2/tutorial/inputoutput.html#old-string-formatting
But as @regilero says, the sentence is gone from 3.3, which might suggest it’s not actually the case. There are some conversations here that suggest the same thing.
As of Python 3.4 the paragraph 7.1.1 reads:
See also Python 3.4 4.7.2 printf-style String Formatting.