Python’s New Template Strings (PEP 750): The Evolution Beyond f-Strings
Python developers love string formatting. It’s everywhere — logs, SQL queries, APIs, HTML rendering, debugging messages, and more.
Python’s New Template Strings (PEP 750): The Evolution Beyond f-Strings
Python developers love string formatting. It’s everywhere — logs, SQL queries, APIs, HTML rendering, debugging messages, and more.
But Python’s most popular formatting tool — f-strings — has some limitations that start showing up in real production systems.
That’s where PEP 750 Template Strings (t-strings) come in. But wait, let’s juggle your memory first. What was PEP again ?
What Are PEPs in Python?
If you’ve worked with Python long enough, you’ve probably seen references like PEP 8, PEP 20, PEP 484, PEP 498, PEP 750. These are Python Enhancement Proposals (PEPs).
A PEP is basically a design document that proposes a change or feature for Python. It explains the motivation, specification, and reasoning behind the change before it becomes part of the language.
The entire Python language evolves through this process.
Some famous examples:
PEP 8 : Python style guide
PEP 20 : The Zen of Python
PEP 484 : Type hints
PEP 498 : f-strings
PEP 750 : Template strings
1. Early Python String Formatting
Before Python 3.6, developers mainly used two approaches.
% Formatting (Old Style)
name = "Alex"
age = 25
print("My name is %s and I am %d years old" % (name, age))
Problems
- Hard to read with many variables
- Error-prone ordering
- Limited flexibility
Example with many variables:
print("User %s has %d messages and %f balance" % (name, 10, 52.34))
Now imagine 8–10 variables. Maintenance becomes painful.
str.format() (Improved but verbose)
name = "Alex"
age = 25
print("My name is {} and I am {} years old".format(name, age))
Better control with named parameters:
print("My name is {name} and I am {age}".format(name=name, age=age))
Still, it requires:
- repetitive syntax
- function calls
- reduced readability in long templates
This is where f-strings changed the game.
2. f-Strings (Python 3.6)
Formatted string literals introduced a much cleaner approach.
name = "Alex"
age = 25
print(f"My name is {name} and I am {age} years old")
Python evaluates expressions inside {} directly.
Example: Expressions inside f-strings
price = 50
tax = 0.18
print(f"Total price: {price * (1 + tax)}")
Output is computed automatically.
Why Developers Prefer f-strings
1. Readability
# Old
"Total: {}".format(price * tax)
# f-string
f"Total: {price * tax}"
2. Debugging Support
Python allows quick variable inspection.
value = 42
print(f"{value=}")
Output:
value=42
This feature is extremely useful during debugging.
3. Formatting Control
pi = 3.141592653
print(f"{pi:.2f}")
Output:
3.14
Interactive Developer Example
Imagine generating logs:
user = "sakshi"
status = "logged in"
time = "21:30"
log = f"[{time}] User {user} {status}"
print(log)
This reads like natural language.
3. t-Strings (Python 3.14)
Python 3.14 introduces template string literals (t-strings).
They look similar to f-strings but serve a different goal: safe template construction.
name = "Alex"
msg = t"Hello {name}"
Instead of immediately producing a string, Python creates a template object.
Developers can process it later.
Why t-strings Exist
Large systems often need safe string templates where evaluation should not happen immediately.
Examples:
- SQL query builders
- HTML templating
- Logging frameworks
- Translation systems
f-strings evaluate immediately, which can sometimes be unsafe.
Example
query = t"SELECT * FROM users WHERE name = {name}"
The system can later safely substitute values.
Conceptually:
Template:
"SELECT * FROM users WHERE name = {name}"
Variables:
{name}
This allows frameworks to:
- validate variables
- sanitize inputs
- prevent injection vulnerabilities
Use f-strings when:
- formatting logs
- building messages
- printing results
- quick debugging
Example:
f"Training accuracy: {accuracy:.2%}"
Use t-strings when:
- building templates
- writing frameworks
- generating SQL/HTML safely
- delaying variable substitution
That’s where PEP 750 Template Strings (t-strings) come in.
메타데이터
- post_id
- 69d9ef72a2fa
- slug
- pythons-new-template-strings-pep-750-the-evolution-beyond-f-strings-69d9ef72a2fa
- url
- https://medium.com/@diksha-writes/pythons-new-template-strings-pep-750-the-evolution-beyond-f-strings-69d9ef72a2fa
- canonical_url
- https://medium.com/@diksha-writes/pythons-new-template-strings-pep-750-the-evolution-beyond-f-strings-69d9ef72a2fa
- author_url
- https://medium.com/@diksha-writes
- status
- ok
- fetched_at
- 2026-07-11 23:42:18