Introduction
Strings are one of the most frequently used types in any Python program. This lesson covers creation, formatting, slicing, and the brand-new template strings in Python 3.14 that give you fine-grained control over interpolation.
Key Concepts
- str: An immutable sequence of Unicode characters.
- F-string: The recommended string formatting syntax, using the
f"..."prefix. - Slice notation:
s[start:stop:step]to extract substrings without copying the original. - Template string (t-string): A Python 3.14 feature that returns a
Templateobject instead of a finished string, enabling safe interpolation.
Real World Context
String handling shows up everywhere: building API responses, formatting log messages, sanitizing user input for SQL queries, and rendering HTML. Choosing the right formatting approach can prevent security vulnerabilities like SQL injection (where t-strings shine) and make your code dramatically more readable.
Deep Dive
String Creation
pythonsingle = 'Hello' double = "World" multiline = '''This is a multiline string''' raw = r"C:\Users\name" # Raw string (no escape processing)
String Methods
Strings have many built-in methods:
pythontext = " Hello World " text.strip() # "Hello World" text.lower() # " hello world " text.upper() # " HELLO WORLD " text.split() # ["Hello", "World"] text.replace("World", "Python") # " Hello Python "
String Formatting
Python offers multiple ways to format strings:
F-Strings (Recommended)
pythonname = "Alice" age = 30 print(f"{name} is {age} years old") print(f"{age:03d}") # "030" (zero-padded) print(f"{3.14159:.2f}") # "3.14" (2 decimal places)
Format Specifiers
python# Width and alignment f"{'left':<10}" # 'left ' f"{'right':>10}" # ' right' f"{'center':^10}" # ' center ' # Number formatting f"{1000000:,}" # '1,000,000' f"{0.5:.0%}" # '50%'
String Indexing and Slicing
pythons = "Python" s[0] # 'P' s[-1] # 'n' s[0:3] # 'Pyt' s[::2] # 'Pto' (every second character) s[::-1] # 'nohtyP' (reversed)
Template Strings (Python 3.14+)
Python 3.14 introduces template strings with the t prefix. Unlike f-strings that immediately produce a string, t-strings return a Template object that lets you inspect and process the interpolated values before combining them:
pythonfrom string.templatelib import Template, Interpolation name = "Alice" template = t"Hello, {name}!" # template is a Template object, not a string # Access parts separately print(template.strings) # ('Hello, ', '!') print(template.interpolations) # (Interpolation('Alice', 'name', None, ''),)
This is useful for SQL sanitization, HTML escaping, and any scenario where you need to process interpolated values safely before combining them.
Common Pitfalls
- Forgetting strings are immutable -- Calling
text.upper()does not changetext; it returns a new string. You must reassign:text = text.upper(). - Using
+for repeated concatenation in loops -- Building a string with+=inside a loop creates a new string on every iteration. Use"\n".join(parts)or anio.StringIObuffer instead. - Confusing f-strings with t-strings -- An f-string (
f"...") produces astrimmediately. A t-string (t"...") produces aTemplateobject. Using one where you mean the other will cause type errors.
Best Practices
- Prefer f-strings for everyday formatting -- They are the most readable and performant option for building strings from variables.
- Use raw strings for regex and Windows paths --
r"\d+"avoids accidental escape sequences and keeps patterns clear. - Use t-strings for user-supplied data -- When building SQL or HTML from untrusted input, t-strings let you sanitize interpolated values before combining them.
Summary
- Strings in Python are immutable Unicode sequences with rich built-in methods.
- F-strings are the recommended formatting approach; format specifiers handle alignment, padding, and number formatting.
- Slice notation (
s[start:stop:step]) provides powerful substring extraction. - Python 3.14 t-strings return
Templateobjects for safe, inspectable interpolation. - Always use
join()over+=in loops and raw strings for regex patterns.
Code Examples
# F-string debugging (3.8+)
x = 10
y = 20
print(f"{x=}, {y=}") # "x=10, y=20"
# F-string expressions
items = ["apple", "banana"]
print(f"Count: {len(items)}") # "Count: 2"
# Nested quotes (3.12+)
data = {"key": "value"}
print(f"Result: {data["key"]}")