@@ -66,6 +66,7 @@ Summary -- release highlights
6666
6767 * :ref: `PEP 649: deferred evaluation of annotations <whatsnew314-pep649 >`
6868* :ref: `PEP 741: Python Configuration C API <whatsnew314-pep741 >`
69+ * :ref: `PEP 750: Template Strings <whatsnew314-pep750 >`
6970* :ref: `PEP 758: Allow except and except* expressions without parentheses <whatsnew314-pep758 >`
7071* :ref: `PEP 761: Discontinuation of PGP signatures <whatsnew314-pep761 >`
7172* :ref: `PEP 765: Disallow return/break/continue that exit a finally block <whatsnew314-pep765 >`
@@ -92,6 +93,75 @@ If you encounter :exc:`NameError`\s or pickling errors coming out of
9293New features
9394============
9495
96+ .. _whatsnew314-pep750 :
97+
98+ PEP 750: Template Strings
99+ -------------------------
100+
101+ Template string literals (t-strings) are a generalization of f-strings,
102+ using a ``t `` in place of the ``f `` prefix. Instead of evaluating
103+ to :class: `str `, t-strings evaluate to a new :class: `string.templatelib.Template ` type:
104+
105+ .. code-block :: python
106+
107+ from string.templatelib import Template
108+
109+ name = " World"
110+ template: Template = t" Hello {name} "
111+
112+ The template can then be combined with functions that operate on the template's
113+ structure to produce a :class: `str ` or a string-like result.
114+ For example, sanitizing input:
115+
116+ .. code-block :: python
117+
118+ evil = " <script>alert('evil')</script>"
119+ template = t" <p>{evil} </p>"
120+ assert html(template) == " <p><script>alert('evil')</script></p>"
121+
122+ As another example, generating HTML attributes from data:
123+
124+ .. code-block :: python
125+
126+ attributes = {" src" : " shrubbery.jpg" , " alt" : " looks nice" }
127+ template = t" <img {attributes} />"
128+ assert html(template) == ' <img src="shrubbery.jpg" alt="looks nice" class="looks-nice" />'
129+
130+ Unlike f-strings, the ``html `` function has access to template attributes
131+ containing the original information: static strings, interpolations, and values
132+ from the original scope. Unlike existing templating approaches, t-strings build
133+ from the well-known f-string syntax and rules. Template systems thus benefit
134+ from Python tooling as they are much closer to the Python language, syntax,
135+ scoping, and more.
136+
137+ Writing template handlers is straightforward:
138+
139+ .. code-block :: python
140+
141+ from string.templatelib import Template, Interpolation
142+
143+ def lower_upper (template : Template) -> str :
144+ """ Render static parts lowercased and interpolations uppercased."""
145+ parts: list[str ] = []
146+ for item in template:
147+ if isinstance (item, Interpolation):
148+ parts.append(str (item.value).upper())
149+ else :
150+ parts.append(item.lower())
151+ return " " .join(parts)
152+
153+ name = " world"
154+ assert lower_upper(t" HELLO {name} " ) == " hello WORLD"
155+
156+ With this in place, developers can write template systems to sanitize SQL, make
157+ safe shell operations, improve logging, tackle modern ideas in web development
158+ (HTML, CSS, etc.), and implement lightweight, custom business DSLs.
159+
160+ See :pep: `750 ` for more details.
161+
162+ (Contributed by Jim Baker, Guido van Rossum, Paul Everitt, Koudai Aono,
163+ Lysandros Nikolaou, and Dave Peck in :gh: `132661 `.)
164+
95165.. _whatsnew314-pep768 :
96166
97167PEP 768: Safe external debugger interface for CPython
0 commit comments