Strings
Prerequisites: Data types
A string is text, written inside single '...' or double "..."
quotes — Python treats them identically, so pick one style and stay
consistent.
Syntax
greeting = "Hello"
Example
name = "Ada"
message = f"Hello, {name}! You have {3 + 2} new messages."
print(message)
print(name.upper())
print(len(name))
Expected output
Hello, Ada! You have 5 new messages.
ADA
3
An f"..." string (an f-string) lets you embed any expression
directly inside { } — this is the standard, modern way to build strings
from variables in Python. Strings also come with useful built-in methods
like .upper(), .lower(), and .strip() (removes surrounding
whitespace).
Common mistakes
- Forgetting the
fbefore the quote —"Hello, {name}"prints the literal text{name}, not Ada's actual name. - Trying to combine text and a number with
+directly:"Age: " + 25raises aTypeError. Use an f-string, or convert the number withstr(25)first.
Practice
Create a variable language = "Python" and print
"I am learning Python!" using an f-string.
Show solution
language = "Python"
print(f"I am learning {language}!")