To loop for infinity (or until a certain condition is met):

while True:
    # Insert code here
    if conditition_to_break:
        break

This will call your code in an endless loop until condition_to_break is True and then breaks out of the loop.

You can read more on while loops here.

If you want to repeat something n times try using a for loop (read more here).

for x in range(3):
    # This code will execute 3 times
    print("Executed {0} times".format(x+1))
Answer from Ewan on Stack Overflow
🌐
Reddit
reddit.com › r/learnpython › repeat function
r/learnpython on Reddit: Repeat function
May 17, 2023 -

Hi, I am very new to python and my current problem is that I want to be able to repeat a certain line of code and not the entire program until something else becomes true, I've done some digging online and cant find a solution that I understand properly, the code pasted has a comment on line 22, that is where I want a repeat function to go, the comment goes more in-depth. Here is a link to the code

https://pastebin.com/M46X2awm

🌐
FavTutor
favtutor.com › blogs › repeat-n-times-python
How to Repeat N times in Python? (& how to Iterate?)
October 25, 2022 - Let's move to it! There isn't any specific repeat function in the builtins module of Python. But Python offers modules like NumPy and pandas, which have their own repeat() function.
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-itertools-repeat
Python - itertools.repeat() - GeeksforGeeks
July 12, 2025 - Syntax: repeat(val, num) Parameters: val: The value to be printed. num: If the optional keyword num is mentioned, then it repeatedly prints the passed value num number of times, otherwise prints the passed value infinite number of times.
🌐
OpenClassrooms
openclassrooms.com › us › courses › 6902811-learn-python-basics › 7090826-easily-repeat-tasks-using-loops
Easily Repeat Tasks Using Loops - OpenClassrooms
December 23, 2020 - Python makes it very easy to loop through every element of a sequence. If you want to print every element in a list, it will look like this: dog_breeds = ["golden retriever", "chihuahua", "terrier", "pug"] for dog in dog_breeds: print(dog) In this code, each element in dog_breeds will be printed to the terminal. dog is a variable name that updates to be the next element every time the loop repeats.
🌐
Educative
educative.io › answers › what-is-the-numpyrepeat-function-in-python
What is the numpy.repeat() function in Python?
The repeat() function in Python is simply used to repeat the elements of an input array.
Find elsewhere
🌐
Python.org
discuss.python.org › python help
Repeat while loop? - Python Help - Discussions on Python.org
October 28, 2021 - Hello! So, Im trying to figure out how to repeat a while loop. I want my loop to restart completely, like if I’m making a game and I want a round two. I’ve tried looking online but it always confused me what is going on……
🌐
Team Treehouse
teamtreehouse.com › community › repeat-an-if-statement-inside-a-function
Repeat an if statement inside a function - Python - Treehouse
June 15, 2016 - Im getting a little confused though after the else statement in the playagain function. How would I make it that on the else statement it should jump back to the beginning of the function and redo the input request?
🌐
YouTube
youtube.com › pixegami
Learn Python • #6 Loops • How to Repeat Code Execution - YouTube
Loops in Python will let you run the same code over and over again.This is useful when working with lists, or if you have something that you want to execute ...
Published   April 1, 2023
🌐
Python
docs.python.org › 3 › library › itertools.html
itertools — Functions creating iterators for efficient looping
To compute the product of an iterable with itself, specify the number of repetitions with the optional repeat keyword argument. For example, product(A, repeat=4) means the same as product(A, A, A, A). This function is roughly equivalent to the ...
🌐
Turing
turing.com › kb › how-to-repeat-string-n-times-in-python
A Guide to Writing Code in Python to Repeat a String N-times
In the code above, the first line collects the string to be repeated and the next line collects the number of times the string will be inputted. In the fourth line, "I" is initialized to be 0. Then, the while loop is written so when "I" is less than 0 for each increment of "I", the string will be printed. In this code, we create a function named repeat_n_times, so whenever the function is called anywhere in the code, it asks for a string to be repeated and the number of times for it to be repeated.
🌐
Quora
quora.com › How-do-I-repeat-a-function-in-Python
How to repeat a function in Python - Quora
Answer (1 of 8): I’m not sure what exactly you mean by “repeat”. If we define a function, it usually means that we want to use that code more than once. When we use a previously defined function, we say that we “call” the function. Functions may be called from just about anywhere ...
🌐
Enki
enki.com › post › how-to-repeat-code-in-python
Enki | Blog - How to Repeat Code in Python
Repeating code in Python using for loops is both powerful and simple, making your code more readable and maintainable. You can iterate over various data structures such as lists, tuples, and dictionaries, or simply repeat an action using the range() function.
🌐
W3Schools
w3schools.com › python › python_for_loops.asp
Python For Loops
The range() function returns a sequence of numbers, starting from 0 by default, and increments by 1 (by default), and ends at a specified number.
🌐
Python documentation
docs.python.org › 3 › tutorial › introduction.html
3. An Informal Introduction to Python — Python 3.14.6 documentation
In the following examples, input and output are distinguished by the presence or absence of prompts (>>> and …): to repeat the example, you must type everything after the prompt, when the prompt appears; lines that do not begin with a prompt are output from the interpreter.
🌐
Reeborg
reeborg.ca › docs › en › basics › repeat.html
14. Repeat — Learn Python with Reeborg
My reason for having repeat as an addition to Python’s standard notation was to avoid having to introduce 4 concepts at the same time (loops, variables as in _ in for _ in range(n), builtin functions like range as well as the concept of function arguments).
🌐
Spark By {Examples}
sparkbyexamples.com › home › python › python numpy repeat() function
Python NumPy repeat() Function - Spark By {Examples}
March 27, 2024 - Python NumPy repeat() function is used to repeat the individual elements of an array a specified number of times. Specify the number of times to repeat by