Template Strings and str.format
Other formatting tools.
Template Strings and str.format is a free Python Academy lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Python Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Other formatting tools
Beyond f-strings, Python offers two more formatting tools: the str.format() method and the string.Template class. Each suits different situations.
print('Hello, {}!'.format('World'))str.format basics
str.format() replaces {} placeholders with arguments, in order. It predates f-strings and still appears in lots of code.
msg = '{} is {} years old'.format('Ada', 36)
print(msg)Positional indexes
Put numbers in the braces to reuse or reorder arguments by position.
print('{0} {1} {0}'.format('echo', 'once'))Named placeholders
Use names inside the braces and pass keyword arguments. This makes templates self-documenting.
print('{name} scored {score}'.format(name='Lin', score=95))Format specs still apply
The same mini-language works after a colon in str.format, so you can control width, precision, and alignment.
print('{:.2f}'.format(3.14159))
print('{:>8}'.format('hi'))Formatting from a dict
Unpack a dictionary into str.format with ** to fill named placeholders.
data = {'city': 'Rome', 'temp': 27}
print('{city}: {temp} C'.format(**data))Introducing Template strings
string.Template uses $-based placeholders. It is intentionally simple and safer for user-supplied templates because it cannot run arbitrary expressions.
from string import Template
t = Template('Hello, $name!')
print(t.substitute(name='World'))Braces for clarity
Wrap a placeholder name in braces, like ${name}, when it sits next to other text.
from string import Template
t = Template('${item}s cost $$5')
print(t.substitute(item='apple'))safe_substitute
substitute raises KeyError for missing names. safe_substitute leaves unknown placeholders untouched instead.
from string import Template
t = Template('$greeting, $name')
print(t.safe_substitute(greeting='Hi'))When to use which
Guidelines: prefer f-strings for everyday code. Use str.format when the template is separate from the data. Use Template for untrusted, user-provided templates.
name = 'Sam'
print(f'f-string: {name}')
print('format: {}'.format(name))Templates from external text
Because the template text is just a string, you can load it from a config file or database, then substitute values at runtime.
from string import Template
stored = 'Order $id ships to $city'
t = Template(stored)
print(t.substitute(id=7, city='Oslo'))Quick Check
Which tool is safest for substituting values into a template provided by an untrusted user?
Recap: Template Strings and str.format
Summary of the alternatives to f-strings:
str.format()uses{}placeholders, supporting positional, named, and the full format spec.string.Templateuses$nameplaceholders and is safe for untrusted input.safe_substituteleaves unknown placeholders in place.- Prefer f-strings for normal code; use these tools when templates are external or untrusted.
from string import Template
print(Template('Bye, $who').substitute(who='all'))
print('Done: {}'.format(True))Frequently asked questions
Is the “Template Strings and str.format” lesson free?
Yes — the full text of “Template Strings and str.format” is free to read here on the web, and the Python Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Python Academy course, upgrade to CoddyKit PRO.
What will I learn in “Template Strings and str.format”?
Other formatting tools. You practise Python Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Python Academy?
No prior experience is required. Python Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Template Strings and str.format” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Python Academy lesson?
Yes. Every Python Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- f-string Expressions
- Format Specifiers
- Format Mini-Language
- Template Strings and str.format