[Python-Dev] PEP 498: Literal String Interpolation

Eric V. Smith eric at trueblade.com
Sun Aug 30 15:41:35 CEST 2015


PEP 498 adds a new string prefix which allows simpler string formatting
by embedding expressions inside string literals. The expressions are
evaluated and inserted into the resulting string value:

>>> import datetime
>>> name = 'Fred'
>>> age = 50
>>> anniversary = datetime.date(1991, 10, 12)

>>> f'My name is {name}, my age next year is {age+1}, my anniversary is
{anniversary:%A, %B %d, %Y}.'
'My name is Fred, my age next year is 51, my anniversary is Saturday,
October 12, 1991.'

There's been a lot of discussion about this on python-ideas, much of
which has been incorporated in to the PEP. Now I feel it's ready for
python-dev input.

Note there's a companion PEP 501 which extends this idea by delaying
converting the expression into a string. This allows for more control
over how the expressions are converted in to strings, and allows for
non-string conversions as well.

I have a complete implementation of PEP 498. I'll shortly create an
issue and attach the patch to it.

-- 
Eric.


More information about the Python-Dev mailing list