How To Print a String Multiple Times in Python

It is possible to use the print() function only 1 time to show a string multiple times on the screen. In this article, we will teach you how to print a string multiple times in Python. Please slide down to learn!

Ways to print a string multiple times in Python

Here are the different ways to print a string multiple times in Python.

Take advantage of the built-in * operator

The * operator allows Python developers to multiply numbers. However, this * also works with a string and an integer. You can take advantage of it to show the print() function how many times you want to print the string on the screen.

Here is the syntax:

print([string]*[integer])

The [string] is what you want to print out. And the [integer] shows how many times you want to print the string.

You can refer to the code sample below:

print("Learn " * 5)

The output will be:

Learn Learn Learn Learn Learn 

You can place \n at the end of the string, inside the "" to print a string multiple times on different lines, as in the code sample below:

print("Learn \n" * 5)

The output will be:

Learn 
Learn 
Learn 
Learn 
Learn 

Use the for loop

Python also features the for loop, which helps developers repeat a command in desired turns. You can also use this for loop to print a string multiple times.

The syntax is like this:

[turn] = 0
for turn in range([integer])   
    print([string])

The [turn] starts from 0. And the [integer] is how many times you want the for loop to work. Each turn, the value of the [turn] will be added by 1. When the for loop finishes, the print() function will stop printing the [string] to the screen.

Here is the code sample:

turn = 0
for turn in range(5):    
  print("Learn")

The output will be:

Learn
Learn
Learn
Learn
Learn

If you want the strings to be on 1 line, you can use end="" as the second parameter of the print() function. Here is the code sample:

turn = 0
for turn in range(5):    
  print("Learn ", end="")

The output will be:

Learn Learn Learn Learn Learn

Use the str.format() function

The str.format() is also a great function you can use to print a string multiple times in Python. The string you use in the str.format() will be moved to the placeholder: {index}. The great thing is you can set as many {index} in the print() function as you like.

Here is the syntax:

print([{0}].format([string]))

Remember that the {0} must be placed inside the "". You use the index 0 inside the {} because the first string in the str.format() has this index.

See the code sample below:

print("{0}{0}{0}{0}".format("Learn "))

The output will be:

Learn Learn Learn Learn 

However, this function is not ideal if you want to print a string a large number of times.

Summary

To summarize, we have shared with you 3 ways to print a string multiple times in Python. Overall, using the * operator is the most convenient way. You just need to multiply a string by the number of times you want to print it out. The other 2 ways are also good. But the for loop seems to take a long time to run, and the str.format() is not ideal for a large number of printing times.

Leave a Reply

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