Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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
2 changes: 2 additions & 0 deletions Doc/glossary.rst
Original file line number Diff line number Diff line change
Expand Up @@ -462,6 +462,7 @@ Glossary
core and with user code.

f-string
f-strings
String literals prefixed with ``f`` or ``F`` are commonly called
"f-strings" which is short for
:ref:`formatted string literals <f-strings>`. See also :pep:`498`.
Expand Down Expand Up @@ -1323,6 +1324,7 @@ Glossary
See also :term:`borrowed reference`.

t-string
t-strings
String literals prefixed with ``t`` or ``T`` are commonly called
"t-strings" which is short for
:ref:`template string literals <t-strings>`.
Expand Down
33 changes: 17 additions & 16 deletions Doc/library/ast.rst
Original file line number Diff line number Diff line change
Expand Up @@ -289,9 +289,9 @@ Literals
* ``conversion`` is an integer:

* -1: no formatting
* 115 (``ord('s')``): ``!s`` string formatting
* 114 (``ord('r')``): ``!r`` repr formatting
* 97 (``ord('a')``): ``!a`` ASCII formatting
* 97 (``ord('a')``): ``!a`` :func:`ASCII <ascii>` formatting
* 114 (``ord('r')``): ``!r`` :func:`repr` formatting
* 115 (``ord('s')``): ``!s`` :func:`string <str>` formatting

* ``format_spec`` is a :class:`JoinedStr` node representing the formatting
of the value, or ``None`` if no format was specified. Both
Expand Down Expand Up @@ -325,14 +325,18 @@ Literals
Constant(value='.3')]))]))


.. class:: TemplateStr(values)
.. class:: TemplateStr(values, /)

A t-string, comprising a series of :class:`Interpolation` and :class:`Constant`
nodes.
.. versionadded:: 3.14

Node representing a template string literal, comprising a series of
:class:`Interpolation` and :class:`Constant` nodes.
These nodes may be any order, and do not need to be interleaved.

.. doctest::

>>> print(ast.dump(ast.parse('t"{name} finished {place:ordinal}"', mode='eval'), indent=4))
>>> expr = ast.parse('t"{name} finished {place:ordinal}"', mode='eval')
>>> print(ast.dump(expr, indent=4))
Expression(
body=TemplateStr(
values=[
Expand All @@ -349,29 +353,26 @@ Literals
values=[
Constant(value='ordinal')]))]))

.. versionadded:: 3.14

.. class:: Interpolation(value, str, conversion, format_spec=None)

.. class:: Interpolation(value, str, conversion, format_spec)
.. versionadded:: 3.14

Node representing a single interpolation field in a t-string.
Node representing a single interpolation field in a template string literal.

* ``value`` is any expression node (such as a literal, a variable, or a
function call).
* ``str`` is a constant containing the text of the interpolation expression.
* ``conversion`` is an integer:

* -1: no conversion
* 115: ``!s`` string conversion
* 114: ``!r`` repr conversion
* 97: ``!a`` ascii conversion
* 97: ``!a`` :func:`ASCII <ascii>` conversion (``ord('a')``)
* 114: ``!r`` :func:`repr` conversion (``ord('r')``)
* 115: ``!s`` :func:`string <str>` conversion (``ord('s')``)

* ``format_spec`` is a :class:`JoinedStr` node representing the formatting
of the value, or ``None`` if no format was specified. Both
``conversion`` and ``format_spec`` can be set at the same time.

.. versionadded:: 3.14


.. class:: List(elts, ctx)
Tuple(elts, ctx)
Expand Down
8 changes: 4 additions & 4 deletions Doc/library/dis.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1122,8 +1122,8 @@ iterations of the loop.

.. opcode:: BUILD_TEMPLATE

Constructs a new :class:`~string.templatelib.Template` from a tuple
of strings and a tuple of interpolations and pushes the resulting instance
Constructs a new :class:`~string.templatelib.Template` instance from a tuple
of strings and a tuple of interpolations and pushes the resulting object
onto the stack::

interpolations = STACK.pop()
Expand All @@ -1135,8 +1135,8 @@ iterations of the loop.

.. opcode:: BUILD_INTERPOLATION (format)

Constructs a new :class:`~string.templatelib.Interpolation` from a
value and its source expression and pushes the resulting instance onto the
Constructs a new :class:`~string.templatelib.Interpolation` instance from a
value and its source expression and pushes the resulting object onto the
stack.

If no conversion or format specification is present, ``format`` is set to
Expand Down
7 changes: 4 additions & 3 deletions Doc/library/stdtypes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2673,9 +2673,10 @@ For example:

The formatting operations described here exhibit a variety of quirks that
lead to a number of common errors (such as failing to display tuples and
dictionaries correctly). Using the newer :ref:`formatted string literals
<f-strings>`, the :meth:`str.format` interface, or :ref:`template strings
($-strings) <template-strings-pep292>` may help avoid these errors.
dictionaries correctly).

Using :ref:`formatted string literals <f-strings>`, the :meth:`str.format`
interface, or :class:`string.Template` may help avoid these errors.
Each of these alternatives provides their own trade-offs and benefits of
simplicity, flexibility, and/or extensibility.

Expand Down
18 changes: 10 additions & 8 deletions Doc/library/string.rst
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ syntax for format strings (although in the case of :class:`Formatter`,
subclasses can define their own format string syntax). The syntax is
related to that of :ref:`formatted string literals <f-strings>` and
:ref:`template string literals <t-strings>`, but it is less sophisticated
and, in particular, does not support arbitrary expressions.
and, in particular, does not support arbitrary expressions in interpolations.

.. index::
single: {} (curly brackets); in string formatting
Expand Down Expand Up @@ -799,13 +799,15 @@ Template strings ($-strings)

.. note::

The feature described here was introduced in Python 2.4. It is unrelated
to, and should not be confused with, the newer
:ref:`template strings <template-strings>` and
:ref:`t-string literal syntax <t-strings>` introduced in Python 3.14.
T-string literals evaluate to instances of a different
:class:`~string.templatelib.Template` class, found in the
:mod:`string.templatelib` module.
The feature described here was introduced in Python 2.4;
a simple templating method based upon regular expressions.
It predates :meth:`str.format`, :ref:`formatted string literals <f-strings>`,
and :ref:`template string literals <template-strings>`.

It is unrelated to template string literals (t-strings),
which were introduced in Python 3.14.
These evaluate to :class:`string.templatelib.Template` objects,
found in the :mod:`string.templatelib` module.

Template strings provide simpler string substitutions as described in
:pep:`292`. A primary use case for template strings is for
Expand Down
Loading
Loading