String concatenation lets you join words or sentences together. Repetition helps you repeat the same word or sentence multiple times easily.
String concatenation and repetition in Python
Start learning this pattern below
Jump into concepts and practice - no test required
or
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Introduction
Syntax
Python
concatenation: string1 + string2 repetition: string * number
Use the plus sign (+) to join two or more strings together.
Use the asterisk (*) to repeat a string a certain number of times.
Examples
Python
full_name = "John" + " " + "Doe" print(full_name)
Python
greeting = "Hi" + ", " + "Alice!" print(greeting)
Python
stars = "*" * 5 print(stars)
Python
laugh = "ha" * 3 print(laugh)
Sample Program
This program joins first and last names with a space, then repeats the word 'Wow! ' three times.
Python
first_name = "Emma" last_name = "Brown" full_name = first_name + " " + last_name print("Full name:", full_name) repeat_word = "Wow! " * 3 print("Repeated word:", repeat_word)
Important Notes
Remember to add spaces if you want spaces between words when concatenating.
Repetition only works with strings and a positive integer number.
Concatenation creates a new string; it does not change the original strings.
Summary
Use + to join strings together.
Use * to repeat a string multiple times.
Both help build new strings easily from smaller parts.
Practice
1. What does the
+ operator do when used with strings in Python?easy
Solution
Step 1: Understand the
In Python,+operator with strings+combines two strings by joining them end to end.Step 2: Compare with other options
Repeating strings uses*, uppercase uses.upper(), splitting uses.split().Final Answer:
Joins two strings together into one -> Option AQuick Check:
String + String = Joined String [OK]
Hint: Remember: + joins strings, * repeats strings [OK]
Common Mistakes:
- Confusing + with * for repetition
- Thinking + changes string case
- Assuming + splits strings
2. Which of the following is the correct syntax to repeat the string
'hi' 3 times in Python?easy
Solution
Step 1: Identify the repetition operator
In Python,*repeats a string when multiplied by an integer.Step 2: Check other options
'hi' + 3causes TypeError (can't add string and int).3 'hi'is SyntaxError (missing * operator).'hi' ** 3causes TypeError (** is for exponents, not string repetition).Final Answer:
'hi' * 3 -> Option BQuick Check:
String * Number = Repeated String [OK]
Hint: Use * between string and number to repeat [OK]
Common Mistakes:
- Using + instead of * for repetition
- Trying to use ** which is invalid for strings
- Placing number before string without *
3. What is the output of the following code?
result = 'ab' + 'cd' * 2 print(result)
medium
Solution
Step 1: Evaluate the repetition part
'cd' * 2 repeats 'cd' twice, resulting in 'cdcd'.Step 2: Concatenate strings
'ab' + 'cdcd' joins to form 'abcdcd'.Final Answer:
abcdcd -> Option DQuick Check:
'ab' + ('cd' * 2) = 'abcdcd' [OK]
Hint: Calculate repetition first, then join strings [OK]
Common Mistakes:
- Adding strings before repeating
- Repeating the whole concatenated string
- Miscounting repeated parts
4. Find the error in this code snippet:
text = 'go' * '3' print(text)
medium
Solution
Step 1: Identify the operands of the * operator
The code tries to multiply a string 'go' by another string '3'.Step 2: Understand Python's type rules for * operator
Python allows string * integer but not string * string, causing a TypeError.Final Answer:
TypeError because string cannot be multiplied by string -> Option AQuick Check:
String * String = TypeError [OK]
Hint: Multiply string only by integer, not by string [OK]
Common Mistakes:
- Using quotes around number for repetition
- Assuming implicit conversion to int
- Ignoring error messages
5. You want to create a string that repeats the word
'yes' 4 times, separated by a dash -. Which code produces 'yes-yes-yes-yes'?hard
Solution
Step 1: Understand the desired output
The string should be 'yes-yes-yes-yes' with 3 dashes separating 4 'yes'.Step 2: Analyze each option
('yes-' * 4)[:-1] repeats 'yes-' 4 times: 'yes-yes-yes-yes-' then removes last dash with [:-1]. 'yes-' * 4 leaves trailing dash. '-'.join('yes' * 4) joins characters, not words. 'yes' + '-' * 3 adds only one dash.Final Answer:
('yes-' * 4)[:-1] -> Option CQuick Check:
Repeat with dash, then remove last dash [OK]
Hint: Repeat with dash, then remove last dash [OK]
Common Mistakes:
- Leaving extra dash at end
- Joining characters instead of words
- Adding fewer dashes than needed
