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.html
Common string operations โ€” Python 3.14.4 documentation
Template strings provide simpler string substitutions as described in PEP 292. 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 ...
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.

๐ŸŒ
Python
docs.python.org โ€บ 3 โ€บ library โ€บ string.templatelib.html
string.templatelib โ€” Support for template string literals
Template strings are a mechanism for custom string processing. They have the full flexibility of Pythonโ€™s f-strings, but return a Template instance that gives access to the static and interpolated (in curly brackets) parts of a string before ...
๐ŸŒ
Python
peps.python.org โ€บ pep-0750
PEP 750 โ€“ Template Strings - Python Enhancement Proposals
July 8, 2024 - This PEP introduces template strings for custom string processing.
๐ŸŒ
InfoWorld
infoworld.com โ€บ home โ€บ software development โ€บ programming languages โ€บ python
How to use template strings in Python 3.14 | InfoWorld
September 26, 2025 - string.templatelib is a new module in the standard library for Python 3.14 that holds the types we need: Template for the type hint to the function, and Interpolation to check the elements we iterate through.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ template-class-in-python
String Template Class in Python - GeeksforGeeks
July 23, 2025 - String Template class from Pythonโ€™s string module provides a simple, $-based way to perform string substitutions. Itโ€™s ideal for user-facing text, email templates and config files.
๐ŸŒ
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?

Find elsewhere
๐ŸŒ
Medium
medium.com โ€บ @bluebirz โ€บ 3-ways-for-python-string-template-71d2bb5d3de1
3 ways for Python string template | by bluebirz | Medium
January 28, 2025 - There is an innate library we can use to format a string in a more flexible way without installing any third-party libraries. It is a string template.
๐ŸŒ
Educative
educative.io โ€บ answers โ€บ string-templates-in-python
String templates in Python
Python's string templates provide a simpler and safer alternative to traditional string formatting methods, enhancing code readability and preventing errors when dealing with user-provided input.
๐ŸŒ
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)
๐ŸŒ
Towards Data Science
towardsdatascience.com โ€บ home โ€บ latest โ€บ python template string formatting method
Python Template String Formatting Method | Towards Data Science
January 21, 2025 - It uses Template class from string module. It has a syntax somewhat similar to .format() when done with keywords, but instead of curly braces to define the placeholder, it utilises a dollar sign ($).
๐ŸŒ
Stack Abuse
stackabuse.com โ€บ formatting-strings-with-the-python-template-class
Formatting Strings with the Python Template Class
September 19, 2021 - The Python Template class is intended to be used for string substitution or string interpolation. The class works using regular expressions and allows for deep customization
๐ŸŒ
Python
peps.python.org โ€บ pep-0501
PEP 501 โ€“ General purpose template literal strings | peps.python.org
August 8, 2015 - However, rather than being rendered directly into a formatted string, these components are instead organised into instances of new types with the following behaviour: class TemplateLiteralText(str): # This is a renamed and extended version of the DecodedConcrete type in PEP 750 # Real type would be implemented in C, this is an API compatible Python equivalent _raw: str def __new__(cls, raw: str): decoded = raw.encode("utf-8").decode("unicode-escape") if decoded == raw: decoded = raw text = super().__new__(cls, decoded) text._raw = raw return text @staticmethod def merge(text_segments:Sequence[
๐ŸŒ
AskPython
askpython.com โ€บ home โ€บ python template strings
Python Template Strings - AskPython
August 6, 2022 - Python's Template String Class provides a way for simple string substitution, wherein the template fields are substituted with suitable replacement strings
๐ŸŒ
Real Python
realpython.com โ€บ python-t-strings
Python 3.14: Template Strings (T-Strings) โ€“ Real Python
July 2, 2025 - The main components of a t-string include static string parts and interpolations, which are accessible through the Template class. You process t-strings by iterating over their components, using attributes such as .strings, .interpolations, and .values for safe and customized handling. Python t-strings enhance both security and flexibility in string processing tasks.
๐ŸŒ
Medium
medium.com โ€บ @julianofischer โ€บ python-3-14-template-strings-0af9d89f7883
Python 3.14 Template Strings. With the release of Python 3.14, aโ€ฆ | by Juliano Fischer Naves | Medium
October 9, 2025 - While f-strings (f"...") immediately return a plain string (str) with all expressions evaluated on the spot, t-strings (t"...") instead produce a Template object that captures the literal and interpolated parts separately without rendering them right away. This structural difference gives t-strings the ability to validate, inspect, or escape values before producing the final output, something f-strings canโ€™t do.
๐ŸŒ
ThoughtCo
thoughtco.com โ€บ pythons-string-templates-2813675
The Power of Python's String Templates
December 31, 2018 - The template itself is a class that receives a string as its argument. The object instantiated from that class is called a template string object. Template strings were first introduced in Python 2.4.
๐ŸŒ
Davepeck
davepeck.org โ€บ 2025 โ€บ 04 โ€บ 11 โ€บ pythons-new-t-strings
Python's new t-strings | Dave Peck
April 11, 2025 - Template strings, also known as t-strings, have been officially accepted as a feature in Python 3.14, which will ship in October 2025.
๐ŸŒ
Python Module of the Week
pymotw.com โ€บ 3 โ€บ string โ€บ index.html
string โ€” Text Constants and Templates
January 28, 2017 - The Quick Brown Fox Jumped Over The Lazy Dog. String templates were added as part of PEP 292 and are intended as an alternative to the built-in interpolation syntax. With string.Template interpolation, variables are identified by prefixing the name with $ (e.g., $var).