Repeat a String N Times
Given a string myStr
and a count n
, we have to concatenate the string myStr
to itself such that the given string appears in the resulting string for n
times.
To repeat given string N times in Python, we can use Multiple Concatenation operator *
. The operator takes the string and the number as operands, and returns a string self-concatenated by given number of times.
Program
In the following program, we take a string myStr
and count in n
variables, and repeat the string myStr
for n
times.
Python Program
myString = 'cherry'
n = 4
output = myString * 4
print(f'Input : {myString}')
print(f'Output : {output}')
Run Code Online Output
Input : cherry
Output : cherrycherrycherrycherry
Summary
In this tutorial of Python Examples, we learned how to repeat a given string for specific number of times, with the help of well detailed examples.
⫷ Previous tutorialNext tutorial ⫸