Q1What is the result of string.Template("Hi $name").substitute(name="Bob")?
string and textwrap — Character Constants, Templates, and Formatting
Use string.ascii_letters / digits and string.Template's $name substitution, plus textwrap.dedent for indent removal and fill for width wrapping on an order email.
What string and textwrap are for
string and textwrap are helper modules that complement everyday string handling. string provides character constants (collections of letters, digits, and symbols) and Template ($name-style template substitution); textwrap handles multi-line string formatting (indent removal and width wrapping). You'll reach for them when f-strings or replace aren't enough.
string — Character Constants and Template Substitution
The string module has two kinds of features. The first is character constants like string.ascii_letters (52 letters), string.digits (0–9), and string.punctuation (symbols), which save you from defining "what counts as a letter / digit" yourself.
The second is string.Template — you embed $name-style $-prefixed variables in a string and later replace them with substitute(name="value"). It looks similar to f-strings (f"{name}"), but Template has a simple, safe parser, which makes it a good fit when the template itself comes from user input (such as email body templates).
$name-style placeholders in a string with values via substitute.| Constant / Class | Contents | Use |
|---|---|---|
| string.ascii_lowercase | abcdefghijklmnopqrstuvwxyz | 26 lowercase letters |
| string.ascii_uppercase | ABCDEFGHIJKLMNOPQRSTUVWXYZ | 26 uppercase letters |
| string.ascii_letters | the two above joined (52 chars) | letter checks, random string generation |
| string.digits | 0123456789 | digit checks |
| string.punctuation | ! " # $ % & ' ... etc | symbol checks |
| string.Template(s) | $name-style template class | email bodies, notification templates |
textwrap — Stripping Triple-Quote Indents and Wrapping Width
textwrap is a standard library module for prettifying strings. The two functions you'll use most are textwrap.dedent (which strips the common leading indent from text written in triple-quoted form) and textwrap.fill (which wraps a long string to a given width).
When you write multi-line strings like """...""" in code, your source-level indent ends up baked into the string itself. dedent removes only the common leading whitespace, so your code stays nicely indented while the string itself comes out clean.
| Function | What it does | Returns |
|---|---|---|
| textwrap.dedent(text) | remove the common leading indent | the cleaned string |
| textwrap.fill(text, width=N) | wrap to width N as one string | single string with newlines |
| textwrap.wrap(text, width=N) | split into lines at width N | list of strings |
| textwrap.shorten(text, w) | if the result wouldn't fit width N, end with [...] | the truncated string |
import textwrap
# dedent: strip the indent from a triple-quoted string
raw = """
Hello.
What a nice day today.
"""
print(textwrap.dedent(raw).strip())
# Hello.
# What a nice day today.
# fill: wrapping (English shows it most clearly)
long_text = "Python is a language that lets you work quickly and integrate systems."
print(textwrap.fill(long_text, width=30))
# Python is a language that
# lets you work quickly and
# integrate systems.
Combine dedent with strip
Triple-quoted strings often start and end with a newline, so the one-liner textwrap.dedent(raw).strip() (dedent + trim surrounding whitespace) is a common pattern. strip is a built-in string method that removes whitespace (newlines included) from both ends.
Knowledge Check
Answer each question one by one.
Q2Which one removes the common leading indent from text written in triple quotes (with source-level indentation)?
Q3What does textwrap.fill(text, width=20) do?