Python strings come with a genuinely useful embedding syntax and a large set of built-in methods — this lesson covers what you'll actually reach for day to day.
An f-string — a string literal prefixed with f — lets you embed any expression directly inside { }:
name = "Priya"
age = 21
print(f"{name} is {age} years old.")
print(f"Next year: {age + 1}") # expressions work too, not just variablesThis lets you skip string concatenation entirely — you can embed a full expression like age + 1, not just a variable name, directly inside the string.
Inside the { }, a colon introduces a format specifier — controlling decimal places, padding, and alignment without a separate formatting function:
price = 3.14159
print(f"{price:.2f}") # "3.14" — 2 decimal places
name = "Amit"
print(f"{name:>10}|") # " Amit|" — right-align in a 10-character field
print(f"{name:<10}|") # "Amit |" — left-align
print(f"{name:^10}|") # " Amit |" — centerYou already saw a variant of this in the Numbers lesson — f"{value:,.2f}" combines a thousands separator with 2 decimal places in a single specifier.
The = debug specifier
Python 3.8 added a debugging shortcut — adding = right after a variable inside { } prints both the variable's name and its value, saving you from typing it out by hand while tracking down a bug.
age = 21
print(f"{age=}") # "age=21"Triple quotes (""" or ''') create a string that can span multiple lines, keeping every line break and space inside it exactly as written — useful for a paragraph of text or a block of formatted output:
message = """Dear Priya,
Your course starts on Monday.
Regards,
LCA"""
print(message)This is the same triple-quote syntax used for multi-line comments in the Syntax Basics lesson — the only difference is whether the result is assigned to something.
Python lets you pull out a range of characters using [start:end] — no separate function needed:
text = "Hello, world!"
print(text[0]) # "H"
print(text[0:5]) # "Hello"
print(text[-1]) # "!" — negative indexes count from the end
print(text[7:]) # "world!" — leaving off the end means "to the end"| Method | What it does | Example |
|---|---|---|
| len(s) | Length of a string (a function, not a method) | len("hello") → 5 |
| s.upper() / s.lower() | Change case | "hi".upper() → "HI" |
| s.strip() | Removes whitespace from both ends | " hi ".strip() → "hi" |
| s.replace(old, new) | Replace all occurrences | "I like cat".replace("cat", "dog") |
| s.split(delimiter) | Split a string into a list | "a,b,c".split(",") → ['a', 'b', 'c'] |
| delimiter.join(list) | Join a list into a string | "-".join(["a", "b"]) → "a-b" |
Methods vs. functions — no clean rule
Notice most of these are methods called on the string itself (s.upper()), not standalone functions taking the string as an argument — except len(), which is a function. This mixed pattern is just something to memorize; there's no rule predicting which is which.
Python has a large standard string method set — see the full string methods reference on python.org for anything not covered here.
String methods return a new string
Python strings are immutable — s.upper() returns a new string, it doesn't change s itself. s.upper() alone, with the result thrown away, is a common beginner mistake; you need s = s.upper() to actually keep the change.
A few more that come up constantly, beyond the table above:
| Method | What it does | Example |
|---|---|---|
| s.startswith(x) / s.endswith(x) | Checks the start/end of a string | "file.pdf".endswith(".pdf") → True |
| s.find(x) | Index of the first match, or -1 if not found | "hello".find("l") → 2 |
| s.count(x) | How many times x appears | "banana".count("a") → 3 |
| s.isdigit() | True if every character is a digit | "123".isdigit() → True |
| s.isalpha() | True if every character is a letter | "abc".isalpha() → True |
| s.title() | Capitalizes the first letter of each word | "hello world".title() → "Hello World" |
| s.zfill(n) | Pads with leading zeros to length n | "7".zfill(3) → "007" |
Between this table and the one above, most everyday string work is covered — anything else is in the full reference linked above.