[Python-ideas] String interpolation for all literal strings
Eric V. Smith
eric at trueblade.com
Wed Aug 5 20:56:52 CEST 2015
In the "Briefer string format" thread, Guido suggested [1] in passing
that it would have been nice if all literal strings had always supported
string interpolation.
I've come around to this idea as well, and I'm going to propose it for
inclusion in 3.6. Once I'm done with my f-string PEP, I'll consider
either modifying it or creating a new (and very similar) PEP.
The concept would be that all strings are scanned for \{ and } pairs. If
any are found, then they'd be interpreted in the same was as the other
discussion on "f-strings". That is, the expression between the \{ and }
would be extracted and searched for conversion characters and format
specifiers. The expression would be evaluated, converted if needed, have
its __format__ method called, and the resulting string inserted back in
to the original string.
Because strings containing \{ are currently valid, we'd have to
introduce this feature with a __future__ import statement. How we
transition to having this be the default interpretation of strings is up
in the air.
Guido privately suggested that it might be nice to also support the 'f'
modifier on strings, to give the identical behavior. This way, you could
start using the feature without requiring the __future__ import. While
I'm not crazy about having two ways to achieve the same thing, I do
think it might be nice to support interpolated strings without requiring
the __future__ import.
Eric.
[1] https://mail.python.org/pipermail/python-ideas/2015-August/034928.html
More information about the Python-ideas
mailing list