Python Repeat String N Times: A Beginner Example – Python Tutorial

By | July 28, 2020

It is very easy to repeat a python string in python. In this tutorial, we will use an example to illustrate python beginners how to do.

Create a python string

text = 'tutorialexample'

Repeat it 3 times

We can use * operation to repeat a python string.

texts = text * 3

Then you will find the texts will be:

tutorialexampletutorialexampletutorialexample

Moreover, if you want to use a character to split the repeated string, you can do like this.

Save string to a python list

c=","

l = []
l.append(text)

List l will save text.

Repeat list n time

texts = l * 3
print(texts)

texts will be:

['tutorialexample', 'tutorialexample', 'tutorialexample']

Join all elements in list

print(c.join(texts))

Then you will get the result:

tutorialexample,tutorialexample,tutorialexample

From this code above, we can find we also can use * to repeat python list.

Leave a Reply

Your email address will not be published. Required fields are marked *