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
string — Common string operations
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 ...
🌐
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 - 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.
Find elsewhere
🌐
Daydreamsoft
daydreamsoft.com › blog › template-string-literals-t-strings-in-python-understanding-pep-750-and-the-future-of-safer-string-formatting
Template String Literals (t-strings) in Python explained with PEP 750, syntax, examples, comparisons with f-strings, security benefits, and practical use cases for developers and modern Python applications.
February 10, 2026 - Learn Python Template String Literals (t-strings) introduced in PEP 750. This blog explains how t-strings work, why they are safer than f-strings, and how developers can use them for secure formatting, SQL queries, logging, and templating. Includes syntax, examples, comparisons, and real-world use cases to help Python developers understand the next evolution of string handling in Python programming.
🌐
Python
peps.python.org › pep-0750
PEP 750 – Template Strings | peps.python.org
July 8, 2024 - This PEP introduces template strings for custom string processing.
🌐
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.
🌐
Python
peps.python.org › pep-3101
PEP 3101 – Advanced String Formatting | peps.python.org
Python currently provides two methods of string interpolation: The ‘%’ operator for strings. [1] The string.Template module.
🌐
Davepeck
davepeck.org › 2025 › 04 › 11 › pythons-new-t-strings
Python's new t-strings | Dave Peck
Template strings, also known as t-strings, have been officially accepted as a feature in Python 3.14, which will ship in October 2025.
🌐
GitConnected
levelup.gitconnected.com › please-dont-deal-with-template-strings-this-way-in-python-71e7a20b6d24
Please Don’t Deal With Template Strings This Way In Python | by Liu Zuo Lin | Level Up Coding
October 21, 2024 - Strings that contain template variables, and that are meant to be used multiple times. For instance: Dear {name}, Here is your bank statement at {bank_acc_number} Yours sincerely, ABC Bank · ^ to use this string, we simply need 2 variables — name and bank_acc_number
🌐
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
🌐
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.
🌐
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).
🌐
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.
🌐
W3Schools
w3schools.com › python › ref_string_format.asp
Python String format() Method
Python Strings Slicing Strings Modify Strings Concatenate Strings Format Strings Escape Characters String Methods String Exercises Code Challenge Python Booleans
🌐
W3Schools
w3schools.com › python › python_strings_format.asp
Python - Format Strings
F-String was introduced in Python 3.6, and is now the preferred way of formatting strings.
🌐
Home Assistant
home-assistant.io › documentation › templating
Templating - Home Assistant
The building blocks of every template, explained in plain language. Making decisions and repeating work with if, for, and friends. Quoting, multi-line strings, and other YAML gotchas. Reading states, attributes, and entity information. Understand data types and how to convert between them. Format, compare, and calculate with dates and times. Cheat sheet of Python methods available in templates.