Concatenation
- Python course
- Beginner
- Lesson 31/116
- 4 min
Learn to join text with + and join.
String concatenation
To join two strings use the + operator. This is called concatenation:
first = "Hello"
second = "World"
print(first + " " + second) # Hello WorldRemember to add a space; + does not add one automatically. Joining many strings with + gets tiring; an f-string is more readable.
String multiplication
You can multiply a string by a number; it repeats the string that many times:
print("ab" * 3) # ababab
print("-" * 10) # ----------This is handy for separators and decorative output. If the number is zero or negative, you get an empty string.
Practice Print concatenation
Assign first = "Hello" and second = "World". Print print(first + " " + second).
Starter code
# String concatenationSample solution
first = "Hello"
second = "World"
print(first + " " + second)Expected output
Hello World
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 Complete the concatenation
print("Hello" + ____ + "World")
print("Hello" + " " + "World")
Explanation: Adding a space gives 'Hello World'.
2 What does "-" * 5 produce?
- -5
- ----- Correct answer
- -----5
- It raises an error
Explanation: String multiplication repeats 5 times: -----.
3 Match the string operations
| Term | Meaning |
|---|---|
+ |
Concatenation |
* |
Repetition |
f"" |
Embedding variables |
len() |
Length |