string.templatelib — Support for template string literals

Source code: Lib/string/templatelib.py


Template strings

Added in version 3.14.

Template strings are a mechanism for custom string processing. They have the full flexibility of Python’s f-strings, but return a Template instance that gives access to the static and interpolated (in curly brackets) parts of a string before they are combined.

To write a t-string, use a 't' prefix instead of an 'f', like so:

>>> pi = 3.14
>>> t't-strings are new in Python {pi!s}!'
Template(
   strings=('t-strings are new in Python ', '!'),
   interpolations=(Interpolation(3.14, 'pi', 's', ''),)
)

Types

class string.templatelib.Template

The Template class describes the contents of a template string. It is immutable, meaning that attributes of a template cannot be reassigned.

The most common way to create a Template instance is to use the template string literal syntax. This syntax is identical to that of