CodeBlanks Logo Icon CodeBlanks

Concatenation

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 World

Remember 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

main.py
# String concatenation

Sample solution

main.py
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

Question
print("Hello" + ____ + "World")
Answer
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

All lessons in Beginner