Skip to content

gh-132661: Document t-strings and templatelib #135229

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions Doc/library/string.rst
Original file line number Diff line number Diff line change
Expand Up @@ -972,3 +972,9 @@ Helper functions
or ``None``, runs of whitespace characters are replaced by a single space
and leading and trailing whitespace are removed, otherwise *sep* is used to
split and join the words.



.. toctree::

string.templatelib.rst
111 changes: 111 additions & 0 deletions Doc/library/string.templatelib.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
:mod:`!string.templatelib` --- Templates and Interpolations for t-strings
=========================================================================

.. module:: string.templatelib
:synopsis: Support for t-string literals.

**Source code:** :source:`Lib/string/templatelib.py`

--------------


.. seealso::

:ref:`f-strings` -- Format strings (f-strings)


.. _templatelib-template:

Template
--------

Check warning on line 20 in Doc/library/string.templatelib.rst

View workflow job for this annotation

GitHub Actions / Docs / Docs

py:class reference target not found: Interpolation [ref.class]

The :class:`Template` class describes the contents of a template string.

The most common way to create a new :class:`Template` instance is to use the t-string literal syntax. This syntax is identical to that of :ref:`f-strings`, except that the string is prefixed with a ``t`` instead of an ``f``. For example, the following code creates a :class:`Template` that can be used to format strings:

>>> name = "World"
>>> greeting = t"Hello {name}!"
>>> type(greeting)
<class 'string.templatelib.Template'>
>>> print(list(greeting))
['Hello ', Interpolation('World'), '!']

It is also possible to create a :class:`Template` directly, using its constructor. This takes an arbitrary collection of strings and :class:`Interpolation` instances:

Check warning on line 33 in Doc/library/string.templatelib.rst

View workflow job for this annotation

GitHub Actions / Docs / Docs

py:class reference target not found: Interpolation [ref.class]

>>> from string.templatelib import Interpolation, Template
>>> name = "World"
>>> greeting = Template("Hello, ", Interpolation(name, "name"), "!")
>>> print(list(greeting))
['Hello, ', Interpolation('World'), '!']

.. class:: Template(*args)

Create a new :class:`Template` object.

:param args: A mix of strings and :class:`Interpolation` instances in any order.

Check warning on line 45 in Doc/library/string.templatelib.rst

View workflow job for this annotation

GitHub Actions / Docs / Docs

py:class reference target not found: Interpolation [ref.class]
:type args: str | Interpolation

If two or more consecutive strings are passed, they will be concatenated into a single value in the :attr:`~Template.strings` attribute. For example, the following code creates a :class:`Template` with a single final string:

>>> from string.templatelib import Template
>>> greeting = Template("Hello ", "World", "!")
>>> print(greeting.strings)
('Hello World!',)

If two or more consecutive interpolations are passed, they will be treated as separate interpolations and an empty string will be inserted between them. For example, the following code creates a template with a single value in the :attr:`~Template.strings` attribute:

>>> from string.templatelib import Interpolation, Template
>>> greeting = Template(Interpolation("World", "name"), Interpolation("!", "punctuation"))
>>> print(greeting.strings)
('', '', '')

.. attribute:: strings

A :ref:`tuple <tut-tuples>` of the static strings in the template.

>>> name = "World"
>>> print(t"Hello {name}!".strings)
('Hello ', '!')

Empty strings *are* included in the tuple:

>>> name = "World"
>>> print(t"Hello {name}{name}!".strings)
('Hello ', '', '!')

.. attribute:: interpolations: tuple[Interpolation, ...]

A tuple of the interpolations in the template.

>>> name = "World"
>>> print(t"Hello {name}!".interpolations)
(Interpolation('World'),)


.. attribute:: values: tuple[Any, ...]

A tuple of all interpolated values in the template.

>>> name = "World"
>>> print(t"Hello {name}!".values)
('World',)

.. method:: __iter__() -> typing.Iterator[str | Interpolation]

Check warning on line 93 in Doc/library/string.templatelib.rst

View workflow job for this annotation

GitHub Actions / Docs / Docs

py:class reference target not found: Interpolation [ref.class]

Iterate over the template, yielding each string and :class:`Interpolation` in order.

Check warning on line 95 in Doc/library/string.templatelib.rst

View workflow job for this annotation

GitHub Actions / Docs / Docs

py:class reference target not found: Interpolation [ref.class]

>>> name = "World"
>>> print(list(t"Hello {name}!"))
['Hello ', Interpolation('World'), '!']

Empty strings are *not* included in the iteration:

>>> name = "World"
>>> print(list(t"Hello {name}{name}"))
['Hello ', Interpolation('World'), Interpolation('World')]






Loading