How to repeat n times in python

Hello guys. How are you all? I hope you all fine. In this tutorial we will learn about How to repeat n times in python. Sometimes we have to repeat some string or number so many times. Its hard to enter them one by one. So by using these methods you can print any string or number N times. So lets learn this by given below methods. I hope this methods will help you. Lets learn this:

How to repeat n times in python

Advertisements

Here to repeat n times in python use this methods. This methods will help you to print the string or number N times. By using these methods you can print a string or number N times. Lets see this methods :

  1. Use range() function
  2. Use itertools.repeat() function
  1. repeat n times in python

    to repeat n times in python just Use range(). By using range() function you can print any string or number N times in python. For this you have to use for loop with range() function. Just make a variable that how many times you want to print a number or string and then use for loop in it. Then print a number or string. Lets learn this by given below example: mynum = 10 for x in range(mynum): print(8) Output :
    8 8 8 8 8 8 8 8 8 8

  2. How to repeat n times in python

    to repeat n times in python just Use itertools.repeat(). By using itertool.repeat() you can repeat N times in python. This method is more efficient than the range() method. It has two parameters. Here first one is None and the second is mynum. mynum is the number which is represent how namy times you want to repeat it. And use it with for loop and then print the string you want to repeat in python. Lets learn this by given below example. Lets learn this.
    import itertools mynum = 10 for _ in itertools.repeat(None, mynum): print('hello guys') Output : hello guys hello guys hello guys hello guys hello guys hello guys hello guys hello guys hello guys hello guys

Method 1: Use range()

Advertisements

By using range() function you can print any string or number N times in python. For this you have to use for loop with range() function. Just make a variable that how many times you want to print a number or string and then use for loop in it. Then print a number or string. Lets learn this by given below example:

mynum = 10
for x in range(mynum):
    print(8)

Output :

8
8
8
8
8
8
8
8
8
8

Method 2: Use itertools.repeat()

Advertisements

By using itertool.repeat() you can repeat N times in python. This method is more efficient than the range() method. It has two parameters. Here first one is None and the second is mynum. mynum is the number which is represent how namy times you want to repeat it. And use it with for loop and then print the string you want to repeat in python. Lets learn this by given below example. Lets learn this.

import itertools
mynum = 10
for _ in itertools.repeat(None, mynum):
    print('hello guys')

Output :

hello guys
hello guys
hello guys
hello guys
hello guys
hello guys
hello guys
hello guys
hello guys
hello guys

Conclusion

Advertisements

So This is all About This Tutorial. Hope This Tutorial Helped You. Thank You.

Also Read,

Advertisements
Siddharth

I am an Information Technology Engineer. I have Completed my MCA And I have 4 Year Plus Experience, I am a web developer with knowledge of multiple back-end platforms Like PHP, Node.js, Python and frontend JavaScript frameworks Like Angular, React, and Vue.

Leave a Comment