Recently, during a Python webinar, a question arose by someone about how to repeat a string multiple times in Python, which made me explore more about this topic. After research, I found three effective ways to achieve this task. In this tutorial, I will share my findings with suitable examples and screenshots.
Repeat a String Multiple Times in Python
Let us learn some important methods to repeat a string multiple times in Python.
Check out How to Split String by Whitespace in Python?
Method 1. Use Multiplication Operator (*)
The simplest way to repeat a string multiple times in Python is by using the * operator. This method is concise and efficient for repeating short strings.
name = "John"
result = name * 3
print(result)
Output:
JohnJohnJohn
I have executed the above example code and added the screenshot below.
This approach is the most readable and is ideal for small-scale string repetition tasks. However, it lacks flexibility for more complex scenarios like conditional repetition.
Read How to Convert a Comma-Separated String to a List in Python?
Method 2. Use itertools.repeat()
The itertools.repeat() function in Python is useful when working with iterators or when we need repeated elements efficiently without creating multiple copies.
from itertools import repeat
result = ''.join(repeat("Emma", 3))
print(result)
Output:
EmmaEmmaEmma
I have executed the above example code and added the screenshot below.
This method is efficient when combined with iteration-based tasks, but it requires join() to form a complete string. It is best suited for cases where repeated elements are processed as an iterable.
Check out How to Remove HTML Tags from a String in Python?
Method 3. Use for Loop
A for loop in Python provides a structured way to repeat a string multiple times, making it useful for scenarios where additional logic (like conditions) is needed.
name = "Michael"
result = ''.join([name for _ in range(3)])
print(result)
Output:
MichaelMichaelMichael
I have executed the above example code and added the screenshot below.
This method offers more flexibility, allowing modifications during iteration. However, it is slightly less efficient than using * due to list comprehension overhead.
Check out How to Remove Characters from a String in Python?
Repeat a String to a Certain Length in Python
Sometimes, you may need to repeat a string until it reaches a desired length. You can calculate the appropriate number of repeats and use the * operator to achieve this task. Here’s an example:
def repeat_to_length(string, length):
repeat_count = (length + len(string) - 1) // len(string)
return string * repeat_count
company_name = "Acme Inc."
header = repeat_to_length(company_name, 20)
print(header) # Output: Acme Inc.Acme Inc.Ac
In this example, the repeat_to_length function calculates the number of repetitions needed to make the string at least as long as the desired length. It then uses the * operator to repeat the string accordingly.
Read How to Convert an Object to a String in Python?
Repeat a Substring in Python
In some cases, you may want to repeat only a portion of a string. Python allows you to easily extract a substring and repeat it using the * operator task. Here’s an example:
def repeat_substring(string, start, end, count):
substring = string[start:end]
return substring * count
address = "123 Main St, Boston, MA"
repeated_city = repeat_substring(address, 12, 18, 3)
print(repeated_city) # Output: BostonBostonBoston
In this example, the repeat_substring function extracts a substring from the address string starting from index 12 (inclusive) to index 18 (exclusive) and repeats it 3 times.
Check out How to Format Decimal Places in Python Using f-Strings?
Conclusion
In this tutorial, We have discussed how to repeat a string multiple times in Python. I explained some important methods, such as using the multiplication operator(*), itertools.repeat(), and for loop. I also covered how to repeat a string to a certain length and repeat a substring in Python.
You may also like to read:
- How to Split a Long String into Multiple Lines in Python?
- How to Extract Numbers from a String in Python?
- How to Pad a String with Zeros in Python?
Bijay Kumar is a 13-time Microsoft MVP with more than 18 years in software development, and the founder of Python Guides and TSinfo Technologies. He started out building .NET and SharePoint solutions at HP, TCS and KPIT before moving into Python, machine learning and AI, and he also builds web apps with TypeScript and React. He writes the tutorials here himself, and every example is run before publishing so you see the real output. More about Bijay · Microsoft MVP profile · LinkedIn