string.templatelib --- Support for template string literals — Types

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 f-strings , except that it uses a t prefix in place of an f

>>> cheese = 'Red Leicester' >>> template = t"We're fresh out of {cheese}, sir." >>> type(template)

Templates are stored as sequences of literal ~Template.strings and dynamic ~Template.interpolations. A ~Template.values attribute holds the values of the interpolations

>>> cheese = 'Camembert' >>> template = t'Ah! We do have {cheese}.' >>> template.strings ('Ah! We do have ', '.') >>> template.interpolations (Interpolation('Camembert', ...),) >>> template.values ('Camembert',)

The !strings tuple has one more element than !interpolations and !values; the interpolations “belong” between the strings. This may be easier to understand when tuples are aligned

Bounded code example (external data; do not execute automatically):
```python
template.strings: ('Ah! We do have ', '.')
template.values: ( 'Camembert', )

.. rubric:: Attributes

.. attribute:: strings

A :class:`tuple` of the static strings in the template.

>>> cheese = 'Camembert'
>>> template = t'Ah! We do have {cheese}.'
>>> template.strings
('Ah! We do have ', '.')

Empty strings *are* included in the tuple:

>>> response = 'We do have '
>>> cheese = 'Camembert'
>>> template = t'Ah! {response}{cheese}.'
>>> template.strings
('Ah! ', '', '.')

The ``strings`` tuple is never empty, and always contains one more
string than the ``interpolations`` and ``values`` tuples:

>>> t''.strings
('',)
>>> t''.values
()
>>> t'{'cheese'}'.strings
('', '')
>>> t'{'cheese'}'.values
('cheese',)

.. attribute:: interpolations

A :class:`tuple` of the interpolations in the template.

>>> cheese = 'Camembert'
>>> template = t'Ah! We do have {
```

The !Interpolation type represents an expression inside a template string. It is immutable, meaning that attributes of an interpolation cannot be reassigned.

Interpolations support pattern matching, allowing you to match against their attributes with the match statement …