You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Documentation forthcoming for PEP 750 template strings, also known as t-strings.
24
-
25
23
.. versionadded:: 3.14
26
24
25
+
Template strings are a generalization of :ref:`f-strings <f-strings>`
26
+
that allow for powerful string processing. The :class:`Template` class
27
+
provides direct access to the static and interpolated (substituted) parts of
28
+
a string.
27
29
28
-
.. _templatelib-template:
29
-
30
-
Template
31
-
--------
32
-
33
-
The :class:`!Template` class describes the contents of a template string.
34
-
35
-
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:
30
+
The most common way to create a :class:`!Template` instance is to use the
31
+
:ref:`t-string literal syntax <t-strings>`. This syntax is identical to that of
32
+
:ref:`f-strings`, except that the string is prefixed with a ``t`` instead of
33
+
an ``f``. For example, the following code creates a :class:`!Template`:
It is also possible to create a :class:`!Template` directly, using its constructor. This takes an arbitrary collection of strings and :class:`Interpolation` instances:
42
+
The :class:`Interpolation` class represents an expression inside a template
43
+
string. It contains the evaluated value of the interpolation (``'World'`` in
44
+
this example), the original expression text (``'name'``), and optional
45
+
conversion and format specification attributes.
45
46
46
-
>>> from string.templatelib import Interpolation, Template
47
+
Templates can be processed in a variety of ways. For instance, here's a
48
+
simple example that converts static strings to lowercase and interpolated
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:
60
96
61
97
>>> from string.templatelib import Template
62
98
>>> greeting = Template("Hello ", "World", "!")
63
-
>>> print(greeting.strings)
99
+
>>> greeting.strings
64
100
('Hello World!',)
65
101
66
102
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:
67
103
68
104
>>> from string.templatelib import Interpolation, Template
:param value: The evaluated, in-scope result of the interpolation.
129
206
:type value: object
130
207
131
-
:param expression: The original *text* of the interpolation's Python :ref:`expressions <expressions>`.
208
+
:param expression: The text of a valid Python expression, or an empty string
132
209
:type expression: str
133
210
134
211
:param conversion: The optional :ref:`conversion <formatstrings>` to be used, one of r, s, and a,.
135
-
:type value: Literal["a", "r", "s"] | None
212
+
:type conversion: Literal["a", "r", "s"] | None
136
213
137
214
:param format_spec: An optional, arbitrary string used as the :ref:`format specification <formatspec>` to present the value.
215
+
:type format_spec: str
138
216
139
-
The :class:`!Interpolation` type represents an expression inside a template string. It is shallow immutable -- its attributes cannot be reassigned.
217
+
The :class:`!Interpolation` type represents an expression inside a template string.
140
218
141
-
>>> name ="World"
142
-
>>> template = t"Hello {name}"
143
-
>>> template.interpolations[0].value
144
-
'World'
145
-
>>> template.interpolations[0].value ="Galaxy"
146
-
Traceback (most recent call last):
147
-
File "<input>", line 1, in <module>
148
-
AttributeError: readonly attribute
219
+
:class:`!Interpolation` instances are shallow immutable: their attributes cannot be
220
+
reassigned.
149
221
150
-
While f-strings and t-strings are largely similar in syntax and expectations, the :attr:`~Interpolation.conversion` and :attr:`~Interpolation.format_spec` behave differently. With f-strings, these are applied to the resulting value automatically. For example, in this ``format_spec``:
222
+
.. property:: value
151
223
152
-
>>> value =42
153
-
>>> f"Value: {value:.2f}"
154
-
'Value: 42.00'
224
+
:returns: The evaluated value of the interpolation.
225
+
:rtype: object
155
226
156
-
With a t-string :class:`!Interpolation`, the template function is expected to apply this to the value:
227
+
>>> t"{1 + 2}".interpolations[0].value
228
+
3
157
229
158
-
>>> value =42
159
-
>>> template = t"Value: {value:.2f}"
160
-
>>> template.interpolations[0].value
161
-
42
230
+
.. property:: expression
162
231
163
-
.. property:: __match_args__
232
+
:returns: The text of a valid Python expression, or an empty string.
233
+
:rtype: str
164
234
165
-
:returns: A tuple of the attributes to use for structural pattern matching.
The :attr:`~Interpolation.expression` is the original text of the
236
+
interpolation's Python expression, if the interpolation was created
237
+
from a t-string literal. Developers creating interpolations manually
238
+
should either set this to an empty string or choose a suitable valid
239
+
Python expression.
167
240
241
+
>>> t"{1 + 2}".interpolations[0].expression
242
+
'1 + 2'
168
243
169
-
.. property:: value
244
+
.. property:: conversion
170
245
171
-
:returns: The evaluated value of the interpolation.
172
-
:rtype:object
246
+
:returns: The conversion to apply to the value, or ``None``
247
+
:rtype:Literal["a", "r", "s"] | None
173
248
174
-
.. property:: expression
249
+
The :attr:`!Interpolation.conversion` is the optional conversion to apply
250
+
to the value:
251
+
252
+
>>> t"{1 + 2!a}".interpolations[0].conversion
253
+
'a'
254
+
255
+
.. note::
256
+
257
+
Unlike f-strings, where conversions are applied automatically,
258
+
the expected behavior with t-strings is that code that *processes* the
259
+
:class:`!Template` will decide how to interpret and whether to apply
260
+
the :attr:`!Interpolation.conversion`.
175
261
176
-
:returns: The original text of the interpolation's Python expression if the interpolation was created from a t-string literal
262
+
.. property:: format_spec
263
+
264
+
:returns: The format specification to apply to the value.
177
265
:rtype: str
178
266
179
-
The :attr:`~Interpolation.expression` is the original text of the interpolation's Python expression, if the interpolation was created from a t-string literal. Developers creating
180
-
interpolations manually should either set this to an empty
181
-
string or choose a suitable valid python expression.
267
+
The :attr:`!Interpolation.format_spec` is an optional, arbitrary string
268
+
used as the format specification to present the value:
182
269
183
-
.. property:: conversion
270
+
>>> t"{1 + 2:.2f}".interpolations[0].format_spec
271
+
'.2f'
184
272
185
-
:returns: The conversion to apply to the value, one of "a", "r", or "s", or None.
186
-
:rtype: Literal["a", "r", "s"] | None
273
+
.. note::
187
274
188
-
The :attr:`~Interpolation.conversion` is the optional conversion to apply to the value. This is one of "a", "r", or "s", or None if no conversion is specified.
275
+
Unlike f-strings, where format specifications are applied automatically
276
+
via the :func:`format` protocol, the expected behavior with
277
+
t-strings is that code that *processes* the :class:`!Template` will
278
+
decide how to interpret and whether to apply the format specification.
279
+
As a result, :attr:`!Interpolation.format_spec` values in
280
+
:class:`!Template` instances can be arbitrary strings, even those that
281
+
do not necessarily conform to the rules of Python's :func:`format`
282
+
protocol.
189
283
190
-
.. property:: format_spec
284
+
.. property:: __match_args__
285
+
286
+
:returns: A tuple of the attributes to use for structural pattern matching.
191
287
192
-
:returns: The format specification to apply to the value.
193
-
:rtype: str
288
+
The tuple returned is ``('value', 'expression', 'conversion', 'format_spec')``.
289
+
This allows for :ref:`pattern matching <match>` on :class:`!Interpolation` instances.
194
290
195
-
The :attr:`~Interpolation.format_spec` is an optional, arbitrary string used as the format specification to present the value. This is similar to the format specification used in :ref:`format strings <formatstrings>`, but it is not limited to a specific set of formats.
0 commit comments