Text of peps.python.org › pep-0750 as Styx crawled it on Nov 19, 2026. The page may have changed since. Only its words are kept, without pictures or layout, and only the first part of a long page.

Open the live page

PEP 750 – Template Strings | peps.python.org

PEP 750 – Template Strings PEP 750 – Template Strings Author : Jim Baker <jim.baker at python.org>, Guido van Rossum <guido at python.org>, Paul Everitt <pauleveritt at me.com>, Koudai Aono <koxudaxi at gmail.com>, Lysandros Nikolaou <lisandrosnik at gmail.com>, Dave Peck <davepeck at davepeck.org> Discussions-To : Discourse thread Status : Final Type : Standards Track Created : 08-Jul-2024 Python-Version : 3.14 Post-History : 09-Aug-2024 , 17-Oct-2024 , 21-Oct-2024 , 18-Nov-2024 Resolution : 10-Apr-2025 Table of Contents Abstract Relationship With Other PEPs Motivation Specification Template String Literals The Template Type The Interpolation Type The Template.values Property Iterating Template Contents Processing Template Strings Template String Concatenation Template and Interpolation Equality No Support for Ordering Support for the debug specifier ( = ) Raw Template Strings Interpolation Expression Evaluation Exceptions No Template.__str__() Implementation The string.templatelib Module Examples Example: Implementing f-strings with t-strings Example: Structured Logging Approach 1: Custom Log Messages Approach 2: Custom Formatters Example: HTML Templating Backwards Compatibility Security Implications How To Teach This Why another templating approach?

Common Patterns Seen in Processing Templates Structural Pattern Matching Memoizing Parsing to Intermediate Representations Context-sensitive Processing of Interpolations Nested Template Strings Approaches to Lazy Evaluation Approaches to Asynchronous Evaluation Approaches to Template Reuse Relation to Format Strings Reference Implementation Rejected Ideas Arbitrary String Literal Prefixes Delayed Evaluation of Interpolations Making Template and Interpolation Into Protocols Overridden __eq__ and __hash__ for Template and Interpolation An Additional Decoded Type The Final Home for Template and Interpolation Enable Full Reconstruction of Original Template Literal Disallowing Template Concatenation Arbitrary Conversion Values Removing conversion From Interpolation Alternate Interpolation Symbols Alternate Layouts for Template Mechanism to Describe the “Kind” of Template Binary Template Strings Acknowledgements Copyright Important This PEP is a historical document.

The up-to-date, canonical documentation can now be found at Template strings . × See PEP 1 for how to propose changes. Abstract This PEP introduces template strings for custom string processing. Template strings are a generalization of f-strings, using a t in place of the f prefix. Instead of evaluating to str , t-strings evaluate to a new type, Template : template : Template = t "Hello { name } " Templates provide developers with access to the string and its interpolated values before they are combined.

This brings native flexible string processing to the Python language and enables safety checks, web templating, domain-specific languages, and more. Relationship With Other PEPs Python introduced f-strings in Python 3.6 with PEP 498 . The grammar was then formalized in PEP 701 which also lifted some restrictions. This PEP is based on PEP 701. At nearly the same time PEP 498 arrived, PEP 501 was written to provide “i-strings” – that is, “interpolation template strings”.

The PEP was deferred pending further experience with f-strings. Work on this PEP was resumed by a different author in March 2023, introducing “t-strings” as template literal strings, and built atop PEP 701. The authors of this PEP consider it to be a generalization and simplification of the updated work in PEP 501. (That PEP has also recently been updated to reflect the new ideas in this PEP.) Motivation Python f-strings are easy to use and very popular.

Over time, however, developers have encountered limitations that make them unsuitable for certain use cases . In particular, f-strings provide no way to intercept and transform interpolated values before they are combined into a final string. As a result, incautious use of f-strings can lead to security vulnerabilities. For example, a user executing a SQL query with sqlite3 may be tempted to use an f-string to embed values into their SQL expression, which could lead to a SQL injection attack .

Or, a developer building HTML may include unescaped user input in the string, leading to a cross-site scripting (XSS) vulnerability. More broadly, the inability to transform interpolated values before they are combined into a final string limits the utility of f-strings in more complex string processing tasks. Template strings address these problems by providing developers with access to the string and its interpolated values.

For example, imagine we want to generate some HTML. Using template strings, we can define an html() function that allows us to automatically sanitize content: evil = "<script>alert('evil')</script>" template = t "<p> { evil } </p>" assert html ( template ) == "<p>&lt;script&gt;alert('evil')&lt;/script&gt;</p>" Likewise, our hypothetical html() function can make it easy for developers to add attributes to HTML elements using a dictionary: attributes = { "src" : "shrubbery.jpg" , "alt" : "looks nice" } template = t "<img { attributes } />" assert html ( template ) == '<img src="shrubbery.jpg" alt="looks nice" />' Neither of these examples is possible with f-strings.

By providing a mechanism to intercept and transform interpolated values, template strings enable a wide range of string processing use cases. Specification Template String Literals This PEP introduces a new string prefix, t , to define template string literals. These literals resolve to a new type, Template , found in the standard library module string.templatelib . The following code creates a Template instance: from string.templatelib import Template template = t "This is a template string." assert isinstance ( template , Template ) Template string literals support the full syntax of PEP 701 .

This includes the ability to nest template strings within interpolations, as well as the ability to use all valid quote marks ( ' , " , ''' , and """ ). Like other string prefixes, the t prefix must immediately precede the quote. Like f-strings, both lowercase t and uppercase T prefixes are supported. Like f-strings, t-strings may not be combined with u or the b prefix. Additionally, f-strings and t-strings cannot be combined, so the ft prefix is invalid. t-strings may be combined with the r prefix; see the Raw Template Strings section below for more information.

The Template Type Template strings evaluate to an instance of a new immutable type, string.templatelib.Template : class Template : strings : tuple [ str , ... ] """ A non-empty tuple of the string parts of the template, with N+1 items, where N is the number of interpolations in the template. """ interpolations : tuple [ Interpolation , ... ] """ A tuple of the interpolation parts of the template. This will be an empty tuple if there are no interpolations.

""" def __new__ ( cls , * args : str | Interpolation ): """ Create a new Template instance. Arguments can be provided in any order. """ ... @property def values ( self ) -> tuple [ object , ... ]: """ Return a tuple of the `value` attributes of each Interpolation in the template. This will be an empty tuple if there are no interpolations. """ ... def __iter__ ( self ) -> Iterator [ str | Interpolation ]: """ Iterate over the string parts and interpolations in the template.

These may appear in any order. Empty strings will not be included. """ ... The strings and interpolations attributes provide access to the string parts and any interpolations in the literal: name = "World" template = t "Hello { name } " assert template . strings [ 0 ] == "Hello " assert template . interpolations [ 0 ] . value == "World" The Interpolation Type The Interpolation type represents an expression inside a template string.

Like Template , it is a new class found in the string.templatelib module: class Interpolation : value : object expression : str conversi

The rest of this page is on the live site.