Joining strings with +
The + operator glues two strings together into a new string, as long as both sides are strings.
Used in 63 lessons of the course, from 00.01.
With + between two strings, Python joins them in order and returns a new string. The originals don't change, because strings are immutable.
Python doesn't add spaces for you. If you want one, put it in yourself: "Hello" + " " + "world".
Both sides must be strings. If one is a number, convert it first with str(), or use an f-string.
The course uses it like this: "| Term | Definition | Taught in |\n|---|---|---|\n" + rows. A fixed header lives in one string and the computed rows in another. The + glues them into the full table that gets displayed.
Reach for it when: you want to glue a few strings together, such as fixed text plus another string you already have in a variable.
Examples
Common mistake
age = 30
print("Age: " + age)Why: + doesn't turn numbers into text on its own. A string and an int can't be joined.
How to write it: Convert with str(age), or write f"Age: {age}".
Common mistake
first = "Ada" last = "Lovelace" print(first + last)
Why: + joins exactly as given, with no space between the two strings.
How to write it: Add the space: first + " " + last.
