@@ -34,7 +34,7 @@ To write a t-string, use a ``'t'`` prefix instead of an ``'f'``, like so:
3434 >>> pi = 3.14
3535 >>> t't-strings are new in Python {pi!s}!'
3636 Template(
37- strings=('t-strings are new in Python ', '! '),
37+ strings=('t-strings are new in Python ', '. '),
3838 interpolations=(Interpolation(3.14, 'pi', 's', ''),)
3939 )
4040
@@ -51,32 +51,32 @@ Types
5151 This syntax is identical to that of :ref: `f-strings <f-strings >`,
5252 except that it uses a ``t `` prefix in place of an ``f ``:
5353
54- >>> name = ' World '
55- >>> template = t' Hello, {name} ! '
54+ >>> cheese = ' Red Leicester '
55+ >>> template = t" We're fresh out of {cheese} , sir. "
5656 >>> type (template)
5757 <class 'string.templatelib.Template'>
5858
5959 Templates are stored as sequences of literal :attr: `~Template.strings `
6060 and dynamic :attr: `~Template.interpolations `.
6161 A :attr: `~Template.values ` attribute holds the values of the interpolations:
6262
63- >>> name = ' World '
64- >>> template = t' Hello, {name} ! '
63+ >>> cheese = ' Camembert '
64+ >>> template = t' Ah! We do have {cheese} . '
6565 >>> template.strings
66- ('Hello, ', '! ')
66+ ('Ah! We do have ', '. ')
6767 >>> template.interpolations
68- (Interpolation('World ', ...),)
68+ (Interpolation('Camembert ', ...),)
6969 >>> template.values
70- ('World ',)
70+ ('Camembert ',)
7171
7272 The :attr: `!strings ` tuple has one more element than :attr: `!interpolations `
7373 and :attr: `!values `; the interpolations “belong” between the strings.
7474 This may be easier to understand when tuples are aligned
7575
7676 .. code-block :: python
7777
78- template.strings: (' Hello, ' , ' ! ' )
79- template.values: ( ' World ' , )
78+ template.strings: (' Ah! We do have ' , ' . ' )
79+ template.values: ( ' Camembert ' , )
8080
8181 .. rubric :: Attributes
8282
@@ -85,17 +85,18 @@ Types
8585
8686 A :class: `tuple ` of the static strings in the template.
8787
88- >>> name = ' World '
89- >>> template = t' Hello, {name} ! '
88+ >>> cheese = ' Camembert '
89+ >>> template = t' Ah! We do have {cheese} . '
9090 >>> template.strings
91- ('Hello, ', '! ')
91+ ('Ah! We do have ', '. ')
9292
9393 Empty strings *are * included in the tuple:
9494
95- >>> name = ' World'
96- >>> template = t' Hello, {name}{name} !'
95+ >>> response = ' We do have '
96+ >>> cheese = ' Camembert'
97+ >>> template = t' Ah! {response}{cheese} .'
9798 >>> template.strings
98- ('Hello, ', '', '! ')
99+ ('Ah! ', '', '. ')
99100
100101 The ``strings `` tuple is never empty, and always contains one more
101102 string than the ``interpolations `` and ``values `` tuples:
@@ -114,26 +115,26 @@ Types
114115
115116 A :class: `tuple ` of the interpolations in the template.
116117
117- >>> name = ' World '
118- >>> template = t' Hello, {name} ! '
118+ >>> cheese = ' Camembert '
119+ >>> template = t' Ah! We do have {cheese} . '
119120 >>> template.interpolations
120- (Interpolation('World ', 'name ', None, ''),)
121+ (Interpolation('Camembert ', 'cheese ', None, ''),)
121122
122123 The ``interpolations `` tuple may be empty and always contains one fewer
123124 values than the ``strings `` tuple:
124125
125- >>> t' Hello! ' .interpolations
126+ >>> t' Red Leicester ' .interpolations
126127 ()
127128
128129 .. attribute :: values
129130 :type: tuple[object, ...]
130131
131132 A tuple of all interpolated values in the template.
132133
133- >>> name = ' World '
134- >>> template = t' Hello, {name} ! '
134+ >>> cheese = ' Camembert '
135+ >>> template = t' Ah! We do have {cheese} . '
135136 >>> template.values
136- ('World ',)
137+ ('Camembert ',)
137138
138139 The ``values `` tuple always has the same length as the
139140 ``interpolations `` tuple. It is always equivalent to
@@ -147,19 +148,21 @@ Types
147148 it is also possible to create them directly using the constructor:
148149
149150 >>> from string.templatelib import Interpolation, Template
150- >>> name = ' World'
151- >>> template = Template(' Hello, ' , Interpolation(name, ' name' ), ' !' )
151+ >>> cheese = ' Camembert'
152+ >>> template = Template(
153+ ... ' Ah! We do have ' , Interpolation(cheese, ' cheese' ), ' .'
154+ ... )
152155 >>> list (template)
153- ['Hello, ', Interpolation('World ', 'name ', None, ''), '! ']
156+ ['Ah! We do have ', Interpolation('Camembert ', 'cheese ', None, ''), '. ']
154157
155158 If multiple strings are passed consecutively, they will be concatenated
156159 into a single value in the :attr: `~Template.strings ` attribute. For example,
157160 the following code creates a :class: `Template ` with a single final string:
158161
159162 >>> from string.templatelib import Template
160- >>> template = Template(' Hello, ' , ' World ' , ' ! ' )
163+ >>> template = Template(' Ah! We do have ' , ' Camembert ' , ' . ' )
161164 >>> template.strings
162- ('Hello, World! ',)
165+ ('Ah! We do have Camembert. ',)
163166
164167 If multiple interpolations are passed consecutively, they will be treated
165168 as separate interpolations and an empty string will be inserted between them.
@@ -168,8 +171,8 @@ Types
168171
169172 >>> from string.templatelib import Interpolation, Template
170173 >>> template = Template(
171- ... Interpolation(' World ' , ' name ' ),
172- ... Interpolation(' ! ' , ' punctuation' ),
174+ ... Interpolation(' Camembert ' , ' cheese ' ),
175+ ... Interpolation(' . ' , ' punctuation' ),
173176 ... )
174177 >>> template.strings
175178 ('', '', '')
@@ -179,30 +182,31 @@ Types
179182 Iterate over the template, yielding each non-empty string and
180183 :class: `Interpolation ` in the correct order:
181184
182- >>> name = ' World '
183- >>> list (t' Hello, {name} ! ' )
184- ['Hello, ', Interpolation('World ', 'name ', None, ''), '! ']
185+ >>> cheese = ' Camembert '
186+ >>> list (t' Ah! We do have {cheese} . ' )
187+ ['Ah! We do have ', Interpolation('Camembert ', 'cheese ', None, ''), '. ']
185188
186189 .. caution ::
187190
188191 Empty strings are **not ** included in the iteration:
189192
190- >>> name = ' World'
191- >>> list (t' Hello, {name}{name} !' ) # doctest: +NORMALIZE_WHITESPACE
192- ['Hello, ',
193- Interpolation('World', 'name', None, ''),
194- Interpolation('World', 'name', None, ''),
195- '!']
193+ >>> response = ' We do have '
194+ >>> cheese = ' Camembert'
195+ >>> list (t' Ah! {response}{cheese} .' ) # doctest: +NORMALIZE_WHITESPACE
196+ ['Ah! ',
197+ Interpolation('We do have ', 'response', None, ''),
198+ Interpolation('Camembert', 'cheese', None, ''),
199+ '.']
196200
197201 .. describe :: template + other
198202 template += other
199203
200204 Concatenate this template with another, returning a new
201205 :class: `!Template ` instance:
202206
203- >>> name = ' World '
204- >>> list (t' Hello ' + t' there {name} ! ' )
205- ['Hello there ', Interpolation('World ', 'name ', None, ''), '! ']
207+ >>> cheese = ' Camembert '
208+ >>> list (t' Ah! ' + t' We do have {cheese} . ' )
209+ ['Ah! We do have ', Interpolation('Camembert ', 'cheese ', None, ''), '. ']
206210
207211 Concatenating a :class: `!Template ` and a ``str `` is **not ** supported.
208212 This is because it is unclear whether the string should be treated as
@@ -212,15 +216,15 @@ Types
212216 (to treat it as a static string)
213217 or use an :class: `!Interpolation ` (to treat it as dynamic):
214218
215- >>> from string.templatelib import Template, Interpolation
216- >>> template = t' Hello '
217- >>> # Treat 'there ' as a static string
218- >>> template += Template(' there ' )
219- >>> # Treat name as an interpolation
220- >>> name = ' World '
221- >>> template += Template(Interpolation(name , ' name ' ))
219+ >>> from string.templatelib import Interpolation, Template
220+ >>> template = t' Ah! '
221+ >>> # Treat 'We do have ' as a static string
222+ >>> template += Template(' We do have ' )
223+ >>> # Treat cheese as an interpolation
224+ >>> cheese = ' Camembert '
225+ >>> template += Template(Interpolation(cheese , ' cheese ' ))
222226 >>> list (template)
223- ['Hello there ', Interpolation('World ', 'name ', None, '')]
227+ ['Ah! We do have ', Interpolation('Camembert ', 'cheese ', None, '')]
224228
225229
226230.. class :: Interpolation
0 commit comments