@@ -162,20 +162,32 @@ <h1 class="font-serif font-bold text-cloudbank text-3xl">
162
162
</ p >
163
163
< p >
164
164
Maybe for some reason you want to render your static parts in
165
- lowercase and your substitions in uppercase?< br /> No problem:
165
+ lowercase and your substitions in uppercase?
166
+ </ p >
167
+ < p >
168
+ No problem:
166
169
</ p >
167
170
< pre > < code class ="language-python "> name = "World"
168
171
my_template = t"Hello {name}!"
169
- parts: list[str] = []
170
- for item in my_template:
171
- if isinstance(item, str):
172
- parts.append(item.lower())
173
- else:
174
- parts.append(item.value.upper())
175
- print("".join(parts))
172
+
173
+ def lower_upper(template: Template) -> str:
174
+ parts: list[str] = []
175
+ for item in template:
176
+ if isinstance(item, str):
177
+ parts.append(item.lower())
178
+ else:
179
+ parts.append(item.value.upper())
180
+ return "".join(parts)
181
+
182
+ print(lower_upper(my_template))
176
183
# "hello WORLD!"
177
184
</ code > </ pre >
178
- < p > Okay, you probably don't want to do exactly < i > that</ i > .</ p >
185
+ < p >
186
+ Okay, you probably don't want to do exactly < i > that</ i > . But this example
187
+ demonstrates that the < i > power of t-strings comes not from the template
188
+ itself, but from the code you (or someone else!) writes to process it
189
+ into a string</ i > .
190
+ </ p >
179
191
< p > So why < i > do</ i > we want t-strings?</ p >
180
192
< p > Let's start with the useful.</ p >
181
193
< p >
@@ -265,8 +277,8 @@ <h1 class="font-serif font-bold text-cloudbank text-3xl">
265
277
266
278
user = get_user(...)
267
279
attribs = {
268
- "id": "user",
269
- "data-id": user.id,
280
+ "id": "user",
281
+ "data-id": user.id,
270
282
"alt": user.name,
271
283
}
272
284
template = t"<div {attribs}>{user.name}</div>"
0 commit comments