How to Print a Statement Any Number of Times in Python



Python


In this article, we show how to print out a statement any number of times you want in Python.

So, basically, to do this, we are going to use a for loop with the keyword range in the statement.

Using this method, we can print out a statement any number of times we want by specifying that number in the range() function.

Below we print out the statement, "hello world" 5 times.



Since i starts with 0, the statement prints out 5 times. i goes from 0 to 1 to 2 to 3 to 4.

In the range() function, the number you specify is exclusive, meaning it is not included in the loop. However, since i begins at an index of 0, it works out perfectly, in that the number you specify in the range() function is the number of times a statement is executed.

So, the code above gives the following output shown below.



To print out "hello world" 10 times, the code would be as that shown below.



The statement, "hello world", now gets printed out 10 times.

If you want to print out, "hello world" 50 times, the code would be that as shown below.



"hello world" now gets printed out 50 times.

Of course, you can execute multiple statements in the for loop.

The code below prints out the statements "It is good" and "Actually it's great" 5 times.



The code produces the output shown below.



And this is all that is required to print a statement any number of times in Python.


Related Resources

How to Show All Tables of a MySQL Database in Python

How to Count the Number of Rows in a MySQL Table in Python



HTML Comment Box is loading comments...