One key advantage of string templates is that you can substitute only some of the placeholders using the safe_substitute method. Normal format strings will raise an error if a placeholder is not passed a value. For example:

"Hello, {first} {last}".format(first='Joe')

raises:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
KeyError: 'last'

But:

from string import Template
Template("Hello, $first $last").safe_substitute(first='Joe')

Produces:

'Hello, Joe $last'

Note that the returned value is a string, not a Template; if you want to substitute the $last you'll need to create a new Template object from that string.

Answer from bwk on Stack Overflow
🌐
Python
docs.python.org › 3 › library › string.templatelib.html
string.templatelib — Support for template string literals
We do have ', 'Camembert', '.') >>> template.strings ('Ah! We do have Camembert.',) If multiple interpolations are passed consecutively, they will be treated as separate interpolations and an empty string will be inserted between them. For example, the following code creates a template with empty placeholders in the strings attribute:
Top answer
1 of 6
41

One key advantage of string templates is that you can substitute only some of the placeholders using the safe_substitute method. Normal format strings will raise an error if a placeholder is not passed a value. For example:

"Hello, {first} {last}".format(first='Joe')

raises:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
KeyError: 'last'

But:

from string import Template
Template("Hello, $first $last").safe_substitute(first='Joe')

Produces:

'Hello, Joe $last'

Note that the returned value is a string, not a Template; if you want to substitute the $last you'll need to create a new Template object from that string.

2 of 6
29

Templates are meant to be simpler than the the usual string formatting, at the cost of expressiveness. The rationale of PEP 292 compares templates to Python's %-style string formatting:

Python currently supports a string substitution syntax based on C's printf() '%' formatting character. While quite rich, %-formatting codes are also error prone, even for experienced Python programmers. A common mistake is to leave off the trailing format character, e.g. the s in %(name)s.

In addition, the rules for what can follow a % sign are fairly complex, while the usual application rarely needs such complexity. Most scripts need to do some string interpolation, but most of those use simple "stringification" formats, i.e. %s or %(name)s This form should be made simpler and less error prone.

While the new .format() improved the situation, it's still true that the format string syntax is rather complex, so the rationale still has its points.

Discussions

Template strings in Python 3.14: an useful new feature or just an extra syntax?
This seems useful to formalize what templating systems already do. More on reddit.com
🌐 r/Python
98
167
May 1, 2025
Coming from JavaScript, what's the python equivalent to string template literals?
not sure why no one is just posting it f'name is {name} and age is {age}' Single or double quotes More on reddit.com
🌐 r/learnpython
18
6
March 26, 2022
is there any difference between using string.format() or an fstring?
Don't forget that f-strings haven't been around forever. It may be partly old habits, it may be not keeping up to date with features, they may still be wanting to target a minimum python version that didn't support f-strings. I'd tend to prefer to use f-strings, but I wouldn't crucify someone for using perfectly valid language constructs. More on reddit.com
🌐 r/Python
145
317
October 9, 2022
jinga2 vs String.template
jinja does things the template strings don't. It can be set up for auto-escaping, it lets you inherit templates from one another, it makes a bit easier to manage whitespace in long templates (not often a big deal, but can be in some contexts) and it's possible to use sandboxed jinja2 to load arbitrary user supplied (and therefore potentially malicious) templates without just handing users the ability to execute arbitrary python. More on reddit.com
🌐 r/learnpython
4
4
October 22, 2021
🌐
GeeksforGeeks
geeksforgeeks.org › python › template-class-in-python
String Template Class in Python - GeeksforGeeks
July 23, 2025 - Example 1: In this example, we substitute a single value in the template. ... Explanation: This code creates a template with the string 'x is $x', where $x is a placeholder.
🌐
Python
docs.python.org › 3 › library › string.html
Common string operations — Python 3.14.4 documentation
A primary use case for template strings is for internationalization (i18n) since in that context, the simpler syntax and functionality makes it easier to translate than other built-in string formatting facilities in Python. As an example of a library built on template strings for i18n, see the flufl.i18n package.
🌐
Towards Data Science
towardsdatascience.com › home › latest › python template string formatting method
Python Template String Formatting Method | Towards Data Science
January 21, 2025 - >>> d = dict(obj='Car') >>> Template('$obj is red').substitute(d) 'Car is red' If there is an invalid string after the placeholder, only the placeholder is considered. Example, see the ‘.’ (dot) after $who.
🌐
Python
peps.python.org › pep-0750
PEP 750 – Template Strings - Python Enhancement Proposals
July 8, 2024 - The string module will be converted into a package, with a new templatelib submodule containing the Template and Interpolation types. Following the implementation of this PEP, this new module may be used for related functions, such as convert(), or potential future template processing code, such as shell script helpers. All examples in this section of the PEP have fully tested reference implementations available in the public pep750-examples git repository.
🌐
Real Python
realpython.com › python-t-strings
Python 3.14: Template Strings (T-Strings) – Real Python
May 30, 2025 - In this example, the first two attempts to define the f-string and t-string fail because the variable number wasn’t defined at that time. After you define this variable, both literals work correctly. In short, template strings are evaluated eagerly from left to right, just like f-strings. This means Python immediately evaluates the input values or expressions when the template string runs.
Find elsewhere
🌐
Tutorialspoint
tutorialspoint.com › python › using_string_template_class.htm
Python - String Template Class
from string import Template temp_str = "My name is $name and I am $age years old" tempobj = Template(temp_str) ret = tempobj.substitute(name='Rajesh', age=23) print (ret) ... We can also unpack the key-value pairs from a dictionary to substitute ...
🌐
Reddit
reddit.com › r/python › template strings in python 3.14: an useful new feature or just an extra syntax?
r/Python on Reddit: Template strings in Python 3.14: an useful new feature or just an extra syntax?
May 1, 2025 -

Python foundation just accepted PEP 750 for template strings, or called t-strings. It will come with Python 3.14.

There are already so many methods for string formatting in Python, why another one??

Here is an article to dicsuss its usefulness and motivation. What's your view?

🌐
InfoWorld
infoworld.com › home › software development › programming languages › python
How to use template strings in Python 3.14 | InfoWorld
September 26, 2025 - If this were a conventional f-string, we could print(template) and get Hello, Davis as the output. But if we try printing a t-string, we don’t get a string. Instead, we get a Python object representation:
🌐
AskPython
askpython.com › home › python template strings
Python Template Strings - AskPython
August 6, 2022 - We have already encountered this in our earlier snipper, where we create our string template object using Template(template_string).
🌐
Stack Abuse
stackabuse.com › formatting-strings-with-the-python-template-class
Formatting Strings with the Python Template Class
September 19, 2021 - If we call substitute() with a set of arguments that doesn't match all the placeholders in our template string, then we'll get a KeyError. If we use an invalid Python identifier in some of our placeholders, then we'll get a ValueError telling us that the placeholder is incorrect. Take this example where we use an invalid identifier, $0name as a placeholder instead of $name.
🌐
Python
peps.python.org › pep-0501
PEP 501 – General purpose template literal strings | peps.python.org
August 8, 2015 - The primary difference between this PEP and PEP 750 is that the latter aims to enable the use of arbitrary string prefixes, rather than requiring the creation of template literal instances that are then passed to other APIs. For example, PEP 750 would allow the sh render described in this PEP to be used as sh"cat {somefile}" rather than requiring the template literal to be created explicitly and then passed to a regular function call (as in sh(t"cat {somefile}")).
🌐
Real Python
realpython.com › python-string-formatting
Python String Formatting: Available Tools and Their Features – Real Python
December 2, 2024 - The different types of string formatting in Python include f-strings for embedding expressions inside string literals, the .format() method for creating string templates and filling them with values, and the modulo operator (%), an older method used in legacy code similar to C’s printf() function.
🌐
Python for Law
pythonforlaw.com › 2021 › 01 › 25 › python-template-strings.html
Using Python Template Strings to Represent Legal Explanations | Python for Law
January 25, 2021 - Here’s an example of a template string used to create a Predicate object in AuthoritySpoke version 0.5: >>> from authorityspoke import Predicate >>> parent_sentence = Predicate("$mother was ${child}'s parent") The phrase that we passed to the Predicate constructor is used to create a Python template string.
🌐
IONOS
ionos.com › digital guide › websites › web development › python string format
How to use Python string format - IONOS
September 4, 2023 - To perform Python string for­mat­ting, we write the string template followed by the modulo operator and the data variable. We also need to assign the formatted string to the message variable: ... The data can be a variable. But we can also use a literal, or an ex­pres­sion. The modulo operation can be placed on a single line. This is an example with a string literal instead of a place­hold­er:
🌐
ThoughtCo
thoughtco.com › pythons-string-templates-2813675
The Power of Python's String Templates
December 31, 2018 - Where string formatting operators used the percentage sign for substitutions, the template object uses dollar signs. $$ is an escape sequence; it is replaced with a single $. $<identifier> names a substitution placeholder matching a mapping key of <identifier>. By default, <identifier> must spell a Python identifier.
🌐
LaunchCode
education.launchcode.org › lchs › chapters › strings › template-literals.html
7.7. Template Literals — LaunchCode's LCHS documentation
Let’s compare the string concatenation in the example above to a template literal: # Concatenation (messy, complicated, hard to read) "Next year, " + name + " will be " + str(current_age + 1) + "." # Template literal (beautiful, simple, easier to read) "Next year, {} will be {}." The braces ...
🌐
Educative
educative.io › answers › string-templates-in-python
String templates in Python
This makes using custom templates for XML files, plain text reports, and HTML web reports feasible. If a variable is missing in .substitute(), it raises a KeyError. To avoid this, use .safe_substitute(). ... Start coding with confidence! Learn Python 3 from Scratch covers Python fundamentals and program structures, culminating in a practical project to solidify your skills. String templates in Python simplify generating dynamic outputs while separating program logic from output formats.