Jason Scheirer's answer is correct, but it could use some more exposition.

First off, to repeat a string an integer number of times, you can use overloaded multiplication:

>>> 'abc' * 7
'abcabcabcabcabcabcabc'

So, to repeat a string until it's at least as long as the length you want, you calculate the appropriate number of repeats and put it on the right-hand side of that multiplication operator:

def repeat_to_at_least_length(s, wanted):
    return s * (wanted//len(s) + 1)

>>> repeat_to_at_least_length('abc', 7)
'abcabcabc'

Then, you can trim it to the exact length you want with an array slice:

def repeat_to_length(s, wanted):
    return (s * (wanted//len(s) + 1))[:wanted]

>>> repeat_to_length('abc', 7)
'abcabca'

Alternatively, as suggested in pillmuncher's answer that probably nobody scrolls down far enough to notice anymore, you can use divmod to compute the number of full repetitions needed, and the number of extra characters, all at once:

def pillmuncher_repeat_to_length(s, wanted):
    a, b = divmod(wanted, len(s))
    return s * a + s[:b]

Which is better? Let's benchmark it:

>>> import timeit
>>> timeit.repeat('scheirer_repeat_to_length("abcdefg", 129)', globals=globals())
[0.3964178159367293, 0.32557755894958973, 0.32851039397064596]
>>> timeit.repeat('pillmuncher_repeat_to_length("abcdefg", 129)', globals=globals())
[0.5276265419088304, 0.46511475392617285, 0.46291469305288047]

So, pillmuncher's version is something like 40% slower, which is too bad, since personally I think it's much more readable. There are several possible reasons for this, starting with its compilation to about 40% more bytecode instructions.

Note: These examples use the newish // operator for truncating integer division. This is often called a Python 3 feature, but according to PEP 238, it was introduced all the way back in Python 2.2. You only have to use it in Python 3 (or in modules that have from __future__ import division), but you can use it regardless.

Answer from zwol on Stack Overflow
Top answer
1 of 16
770

Jason Scheirer's answer is correct, but it could use some more exposition.

First off, to repeat a string an integer number of times, you can use overloaded multiplication:

>>> 'abc' * 7
'abcabcabcabcabcabcabc'

So, to repeat a string until it's at least as long as the length you want, you calculate the appropriate number of repeats and put it on the right-hand side of that multiplication operator:

def repeat_to_at_least_length(s, wanted):
    return s * (wanted//len(s) + 1)

>>> repeat_to_at_least_length('abc', 7)
'abcabcabc'

Then, you can trim it to the exact length you want with an array slice:

def repeat_to_length(s, wanted):
    return (s * (wanted//len(s) + 1))[:wanted]

>>> repeat_to_length('abc', 7)
'abcabca'

Alternatively, as suggested in pillmuncher's answer that probably nobody scrolls down far enough to notice anymore, you can use divmod to compute the number of full repetitions needed, and the number of extra characters, all at once:

def pillmuncher_repeat_to_length(s, wanted):
    a, b = divmod(wanted, len(s))
    return s * a + s[:b]

Which is better? Let's benchmark it:

>>> import timeit
>>> timeit.repeat('scheirer_repeat_to_length("abcdefg", 129)', globals=globals())
[0.3964178159367293, 0.32557755894958973, 0.32851039397064596]
>>> timeit.repeat('pillmuncher_repeat_to_length("abcdefg", 129)', globals=globals())
[0.5276265419088304, 0.46511475392617285, 0.46291469305288047]

So, pillmuncher's version is something like 40% slower, which is too bad, since personally I think it's much more readable. There are several possible reasons for this, starting with its compilation to about 40% more bytecode instructions.

Note: These examples use the newish // operator for truncating integer division. This is often called a Python 3 feature, but according to PEP 238, it was introduced all the way back in Python 2.2. You only have to use it in Python 3 (or in modules that have from __future__ import division), but you can use it regardless.

2 of 16
83

Use:

def repeat_to_length(string_to_expand, length):
   return (string_to_expand * ((length/len(string_to_expand))+1))[:length]

For Python 3:

def repeat_to_length(string_to_expand, length):
    return (string_to_expand * (int(length/len(string_to_expand))+1))[:length]
๐ŸŒ
CodeChef
codechef.com โ€บ learn โ€บ course โ€บ python โ€บ LTCPY08 โ€บ problems โ€บ PYTHCL37A
String repetition in Python Programming
string = "hello " result = string * 3 print(result) # output: hello hello hello # string is assigned the value `hello`. # string * 3 creates a new string by repeating `hello` three times.
Discussions

Repeat a string
Your calling your function, right? repeat(input_, n, delim) More on reddit.com
๐ŸŒ r/learnpython
22
0
September 26, 2022
The repeat method should take a string, and print it a specified number of times. Use loop and break to complete the met
Carolyn Buck is having issues with: The repeat method should take a string, and print it a specified number of times. Use loop and break to complete the method. Be sure to do the f... More on teamtreehouse.com
๐ŸŒ teamtreehouse.com
5
May 5, 2017
python - How to repeat a string multiple times with spaces? - Stack Overflow
I want to pass one string with n times to repeat it with spaces, my code look like: def repeating(word, n): return word * n I want the output look like: hello hello hello with spaces between each More on stackoverflow.com
๐ŸŒ stackoverflow.com
python - Repeat string to certain length - Stack Overflow
What is an efficient way to repeat a string to a certain length? Eg: repeat('abc', 7) -> 'abcabca' Here is my current code: def repeat(string, length): cur, old = 1, string while len(s... More on stackoverflow.com
๐ŸŒ stackoverflow.com
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ how-to-repeat-a-string-in-python
How to Repeat a String in Python? - GeeksforGeeks
July 23, 2025 - Using Multiplication operator (*) is the simplest and most efficient way to repeat a string in Python.
๐ŸŒ
PhoenixNAP
phoenixnap.com โ€บ home โ€บ kb โ€บ devops and development โ€บ how to repeat a string in python?
How to Repeat a String in Python? | phoenixNAP KB
December 22, 2025 - The itertools library contains various iteration functionalities for Python. The itertools.repeat() function creates an iterable object that repeats a string for a specified number of iterations.
๐ŸŒ
Reddit
reddit.com โ€บ r/learnpython โ€บ repeat a string
r/learnpython on Reddit: Repeat a string
September 26, 2022 -

Write a program with a function def repeat(string, n, delim) that returns the string string repeated n times, separated by the string delim (ex. space, ?, #, etc).

Ex: If the input is:

Enter a string: hello How many repetitions? 5 Separated by? # then the output is:

hello#hello#hello#hello#hello Your program will also need a main function to receive input, call the function, and output the results.

My code def repeat(word, n, delim): print(delim.join(word for _ in range(n)))

It produces no output tho I donโ€™t know why

๐ŸŒ
Rosetta Code
rosettacode.org โ€บ wiki โ€บ Repeat_a_string
Repeat a string - Rosetta Code
3 weeks ago - This program defines a subroutine that expects to find a string and a number of times to repeat it; but all it then does is loop and concatenate, so making it a separate subroutine is arguably overkill. 10 LET S$="HA" 20 LET N=5 30 GOSUB 60 40 PRINT T$ 50 STOP 60 LET T$="" 70 FOR I=1 TO N 80 LET T$=T$+S$ 90 NEXT I 100 RETURN ยท FUNCTION StringRepeat$ (s$, n) LET cad$ = "" FOR i = 1 TO n LET cad$ = cad$ & s$ NEXT i LET StringRepeat$ = cad$ END FUNCTION PRINT StringRepeat$("rosetta", 1) PRINT StringRepeat$("ha", 5) PRINT StringRepeat$("*", 5) END
Find elsewhere
๐ŸŒ
Medium
medium.com โ€บ @rajanjain86 โ€บ repeat-after-me-the-powerful-python-string-trick-you-need-to-know-a7660b3e2345
Repeat After Me: The Powerful Python String Trick You Need to Know | by Rajan Jain | Medium
October 21, 2025 - Most of us learn that the asterisk (*) is for multiplication. 5 * 5 gives you 25. But in Python, when you use it with a string and an integer, it does something magical: it repeats the string exactly that many times.
๐ŸŒ
Turing
turing.com โ€บ kb โ€บ how-to-repeat-string-n-times-in-python
A Guide to Writing Code in Python to Repeat a String N-times
There are different ways this can be done. One is with the use of an asterisk(*) which can multiply the number of times a string appears. Another popular way is what we will explore in this article.
๐ŸŒ
Codewars
codewars.com โ€บ kata โ€บ 57a0e5c372292dd76d000d7e
String repeat | Codewars
August 2, 2016 - Write a program which accepts a single byte `n` and then a sequence of bytes `string` and outputs the `string` exactly `n` times. The first input byte will be `n`. Following bytes will be characte...
๐ŸŒ
LeetCode
leetcode.com โ€บ problems โ€บ first-unique-character-in-a-string
First Unique Character in a String - LeetCode
Can you solve this real interview question? First Unique Character in a String - Given a string s, find the first non-repeating character in it and return its index. If it does not exist, return -1. Example 1: Input: s = "leetcode" Output: 0 Explanation: The character 'l' at index 0 is the ...
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ create-multiple-copies-of-a-string-in-python-by-using-multiplication-operator
Create multiple copies of a string in Python by using multiplication operator - GeeksforGeeks
July 23, 2025 - In Python, creating multiple copies of a string can be done in a simple way using multiplication operator. In this article, we will focus on how to achieve this efficiently. This method is the easiest way to repeat a string multiple times in Python.
๐ŸŒ
Pandas
pandas.pydata.org โ€บ docs โ€บ reference โ€บ api โ€บ pandas.Series.str.repeat.html
pandas.Series.str.repeat โ€” pandas 3.0.2 documentation
Duplicates each string in the Series or Index, either by applying the same repeat count to all elements or by using different repeat values for each element.
๐ŸŒ
Medium
medium.com โ€บ @pivajr โ€บ pythonic-tips-how-to-repeat-strings-in-python-d73698942711
Pythonic Tips: How to Repeat Strings in Python | by Dilermando Piva Junior | Medium
March 7, 2025 - s * qtd: In Python, you can multiply a string by an integer to repeat it multiple times. In the example above, the string s is repeated qtd times.
Top answer
1 of 16
770

Jason Scheirer's answer is correct but could use some more exposition.

First off, to repeat a string an integer number of times, you can use overloaded multiplication:

>>> 'abc' * 7
'abcabcabcabcabcabcabc'

So, to repeat a string until it's at least as long as the length you want, you calculate the appropriate number of repeats and put it on the right-hand side of that multiplication operator:

def repeat_to_at_least_length(s, wanted):
    return s * (wanted//len(s) + 1)

>>> repeat_to_at_least_length('abc', 7)
'abcabcabc'

Then, you can trim it to the exact length you want with an array slice:

def repeat_to_length(s, wanted):
    return (s * (wanted//len(s) + 1))[:wanted]

>>> repeat_to_length('abc', 7)
'abcabca'

Alternatively, as suggested in pillmod's answer that probably nobody scrolls down far enough to notice anymore, you can use divmod to compute the number of full repetitions needed, and the number of extra characters, all at once:

def pillmod_repeat_to_length(s, wanted):
    a, b = divmod(wanted, len(s))
    return s * a + s[:b]

Which is better? Let's benchmark it:

>>> import timeit
>>> timeit.repeat('scheirer_repeat_to_length("abcdefg", 129)', globals=globals())
[0.3964178159367293, 0.32557755894958973, 0.32851039397064596]
>>> timeit.repeat('pillmod_repeat_to_length("abcdefg", 129)', globals=globals())
[0.5276265419088304, 0.46511475392617285, 0.46291469305288047]

So, pillmod's version is something like 40% slower, which is too bad, since personally I think it's much more readable. There are several possible reasons for this, starting with its compiling to about 40% more bytecode instructions.

Note: these examples use the new-ish // operator for truncating integer division. This is often called a Python 3 feature, but according to PEP 238, it was introduced all the way back in Python 2.2. You only have to use it in Python 3 (or in modules that have from __future__ import division) but you can use it regardless.

2 of 16
83
def repeat_to_length(string_to_expand, length):
   return (string_to_expand * ((length/len(string_to_expand))+1))[:length]

For python3:

def repeat_to_length(string_to_expand, length):
    return (string_to_expand * (int(length/len(string_to_expand))+1))[:length]