Skip to content

Commit f71ac27

Browse files
authored
Merge pull request #16 from gtm19/main
Update example in t-strings help
2 parents ad1b57d + 17566db commit f71ac27

File tree

1 file changed

+23
-11
lines changed

1 file changed

+23
-11
lines changed

public/introduction.html

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -162,20 +162,32 @@ <h1 class="font-serif font-bold text-cloudbank text-3xl">
162162
</p>
163163
<p>
164164
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:
166169
</p>
167170
<pre><code class="language-python">name = "World"
168171
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))
176183
# "hello WORLD!"
177184
</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>
179191
<p>So why <i>do</i> we want t-strings?</p>
180192
<p>Let's start with the useful.</p>
181193
<p>
@@ -265,8 +277,8 @@ <h1 class="font-serif font-bold text-cloudbank text-3xl">
265277

266278
user = get_user(...)
267279
attribs = {
268-
"id": "user",
269-
"data-id": user.id,
280+
"id": "user",
281+
"data-id": user.id,
270282
"alt": user.name,
271283
}
272284
template = t"&lt;div {attribs}&gt;{user.name}&lt;/div&gt;"

0 commit comments

Comments
 (0)