Python String Operators: Concatenation
and Replication
1. Explanation
➤ Concatenation (+ operator): Used to join two or more strings.
Example:
str1 = "Hello"
str2 = "World"
result = str1 + " " + str2
Output: Hello World
➤ Replication (* operator): Used to repeat a string multiple times.
Example:
text = "Hi! "
result = text * 3
Output: Hi! Hi! Hi!
2. Practice Questions with Answers
Level 1: Basic Practice
1. Concatenate two strings:
str1 = "Good"
str2 = "Morning"
Answer: Good Morning
2. Repeat a string 4 times:
str1 = "Bye! "
Answer: Bye! Bye! Bye! Bye!
3. Concatenate first name and last name:
first = "Alice"
last = "Johnson"
Answer: Alice Johnson
Level 2: Apply & Output Prediction
4. Output of print("Python" + "3")
Answer: Python3
5. Output of print("Go! " * 5)
Answer: Go! Go! Go! Go! Go!
6. Fix the error:
age = 15
print("My age is " + age)
Answer: print("My age is " + str(age))
Level 3: Creative Tasks
7. Create a pattern:
Answer:
HelloHelloHello
WorldWorld
8. Form a sentence:
Input: name = "Liam", hobby = "painting"
Answer: Hello Liam! I heard you like painting.
Bonus Challenge
9. Repeat name with dashes:
Input: Arya
Answer: Arya-Arya-Arya
10. Repeat word by number:
Input: Hi, 4
Answer: HiHiHiHi