Skip to content

Commit 6468a97

Browse files
committed
Adjust t-string docs: move evaluation rules out, add a note on grammar
1 parent f2db8f9 commit 6468a97

File tree

3 files changed

+65
-41
lines changed

3 files changed

+65
-41
lines changed

Doc/library/stdtypes.rst

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2640,6 +2640,46 @@ replacement field. For example::
26402640
>>> f'{one_third = :~>10}~'
26412641
'one_third = ~~~~~~~1/3~'
26422642

2643+
.. _stdtypes-tstrings:
2644+
2645+
Template String Literals (t-strings)
2646+
------------------------------------
2647+
2648+
An :dfn:`t-string` (formally a :dfn:`template string literal`) is
2649+
a string literal that is prefixed with ``t`` or ``T``.
2650+
2651+
These strings follow the same syntax and evaluation rules as
2652+
:ref:`formatted string literals <stdtypes-fstrings>`,
2653+
with for the following differences:
2654+
2655+
* Rather than evaluating to a ``str`` object, template string literals evaluate
2656+
to a :class:`string.templatelib.Template` object.
2657+
2658+
* The :func:`format` protocol is not used.
2659+
Instead, the format specifier and conversions (if any) are passed to
2660+
a new :class:`~string.templatelib.Interpolation` object that is created
2661+
for each evaluated expression.
2662+
It is up to code that processes the resulting :class:`~string.templatelib.Template`
2663+
object to decide how to handle format specifiers and conversions.
2664+
2665+
* Format specifiers containing nested replacement fields are evaluated eagerly,
2666+
prior to being passed to the :class:`~string.templatelib.Interpolation` object.
2667+
For instance, an interpolation of the form ``{amount:.{precision}f}`` will
2668+
evaluate the inner expression ``{precision}`` to determine the value of the
2669+
``format_spec`` attribute.
2670+
If ``precision`` were to be ``2``, the resulting format specifier
2671+
would be ``'.2f'``.
2672+
2673+
* When the equals sign ``'='`` is provided in an interpolation expression,
2674+
the text of the expression is appended to the literal string that precedes
2675+
the relevant interpolation.
2676+
This includes the equals sign and any surrounding whitespace.
2677+
The :class:`!Interpolation` instance for the expression will be created as
2678+
normal, except that :attr:`~string.templatelib.Interpolation.conversion` will
2679+
be set to '``r``' (:func:`repr`) by default.
2680+
If an explicit conversion or format specifier are provided,
2681+
this will override the default behaviour.
2682+
26432683

26442684
.. _old-string-formatting:
26452685

Doc/reference/expressions.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ Formally:
174174
.. grammar-snippet::
175175
:group: python-grammar
176176

177-
strings: ( `STRING` | fstring)+ | tstring+
177+
strings: ( `STRING` | `fstring`)+ | `tstring`+
178178

179179
This feature is defined at the syntactical level, so it only works with literals.
180180
To concatenate string expressions at run time, the '+' operator may be used::

Doc/reference/lexical_analysis.rst

Lines changed: 24 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1080,8 +1080,24 @@ even if they do not include expressions::
10801080
* :meth:`str.format`, which uses a related format string mechanism.
10811081

10821082

1083+
.. _t-strings:
1084+
.. _template-string-literals:
1085+
1086+
t-strings
1087+
---------
1088+
1089+
.. versionadded:: 3.14
1090+
1091+
A :dfn:`template string literal` or :dfn:`t-string` is a string literal
1092+
that is prefixed with '``t``' or '``T``'.
1093+
These strings follow the same syntax rules as
1094+
:ref:`formatted string literals <f-strings>`.
1095+
For differences in evaluation rules, see the
1096+
:ref:`Standard Library section on t-strings <stdtypes-tstrings>`
1097+
1098+
10831099
Formal grammar for f-strings
1084-
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1100+
----------------------------
10851101

10861102
F-strings are handled partly by the :term:`lexical analyzer`, which produces the
10871103
tokens :py:data:`~token.FSTRING_START`, :py:data:`~token.FSTRING_MIDDLE`
@@ -1115,7 +1131,7 @@ The ``FSTRING_MIDDLE`` definition uses
11151131
to indicate special characters (backslash, newline, ``{``, ``}``) and
11161132
sequences (``f_quote``).
11171133

1118-
.. grammar-snippet:: python-grammar
1134+
.. grammar-snippet::
11191135
:group: python-grammar
11201136

11211137
fstring: `FSTRING_START` `fstring_middle`* `FSTRING_END`
@@ -1158,47 +1174,15 @@ sequences (``f_quote``).
11581174
Constructing a more traditional formal grammar from this template is left
11591175
as an exercise for the reader.
11601176

1177+
The grammar for t-strings is identical to the one for f-strings, with *t*
1178+
instead of *f* at the beginning of rule and token names and in the prefix.
11611179

1162-
.. _t-strings:
1163-
.. _template-string-literals:
1164-
1165-
t-strings
1166-
---------
1180+
.. grammar-snippet::
1181+
:group: python-grammar
11671182

1168-
.. versionadded:: 3.14
1183+
tstring: `TSTRING_START` `tstring_middle`* `TSTRING_END`
11691184

1170-
A :dfn:`template string literal` or :dfn:`t-string` is a string literal
1171-
that is prefixed with '``t``' or '``T``'.
1172-
These strings follow the same syntax and evaluation rules as
1173-
:ref:`formatted string literals <f-strings>`, with the following differences:
1174-
1175-
* Rather than evaluating to a ``str`` object, template string literals evaluate
1176-
to a :class:`string.templatelib.Template` object.
1177-
1178-
* The :func:`format` protocol is not used.
1179-
Instead, the format specifier and conversions (if any) are passed to
1180-
a new :class:`~string.templatelib.Interpolation` object that is created
1181-
for each evaluated expression.
1182-
It is up to code that processes the resulting :class:`~string.templatelib.Template`
1183-
object to decide how to handle format specifiers and conversions.
1184-
1185-
* Format specifiers containing nested replacement fields are evaluated eagerly,
1186-
prior to being passed to the :class:`~string.templatelib.Interpolation` object.
1187-
For instance, an interpolation of the form ``{amount:.{precision}f}`` will
1188-
evaluate the inner expression ``{precision}`` to determine the value of the
1189-
``format_spec`` attribute.
1190-
If ``precision`` were to be ``2``, the resulting format specifier
1191-
would be ``'.2f'``.
1192-
1193-
* When the equals sign ``'='`` is provided in an interpolation expression,
1194-
the text of the expression is appended to the literal string that precedes
1195-
the relevant interpolation.
1196-
This includes the equals sign and any surrounding whitespace.
1197-
The :class:`!Interpolation` instance for the expression will be created as
1198-
normal, except that :attr:`~string.templatelib.Interpolation.conversion` will
1199-
be set to '``r``' (:func:`repr`) by default.
1200-
If an explicit conversion or format specifier are provided,
1201-
this will override the default behaviour.
1185+
<rest of the t-string grammar is omitted; see above>
12021186

12031187

12041188
.. _numbers:

0 commit comments

Comments
 (0)