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 is Python's equivalent of PHP's double-quote interpolation, but more powerful — PHP can't embed an expression like age + 1 directly inside a string the way this can.
Python lets you pull out a range of characters using [start:end] — no separate function needed like PHP's substr():
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 the way PHP's strtoupper($s) does — 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.