You often need to run the same block of code a fixed number of times—ten retries, five samples, or n steps of a simulation. In Python the usual pattern is for with range, optionally discarding the index with _. This page shows how to loop exactly n times, count from 1 through n, mirror the logic with while, repeat strings, use itertools.repeat, and avoid off-by-one mistakes.
For the full story on for, see Python for loop. For how range endpoints work, see Python range. For condition-driven repetition, see Python while loop.
Tested on: Python 3.13.3; kernel 6.14.0-37-generic.
Best way to loop N times in Python
When the repeat count is known up front, prefer a for loop over range and throw away the index if you do not need it:
n = 5
for _ in range(n):
print("tick")That prints tick on five lines. The underscore tells readers the loop variable is intentionally unused.
Loop N times using for range()
range(stop) yields 0, 1, …, stop - 1, so the loop runs stop times. range(start, stop) stops before stop; range(start, stop, step) adds a step (for example only even indices).
for i in range(4):
print(i, end=" ")
print()That prints 0 1 2 3 on one line—four iterations.
Loop 10 times in Python
To run a body exactly ten times with a counter from 0 to 9:
for i in range(10):
print(i, end=" ")
print()That prints the digits 0 through 9 separated by spaces.
Loop 5 times or 3 times in Python
Change only the argument to range:
for _ in range(5):
print("five")
for _ in range(3):
print("three")You get five lines containing five and three lines containing three.
Loop N times without using the index variable
Use _ (or any name you ignore) so linters and readers know the index is not part of the logic:
for _ in range(3):
print("no index")That prints no index three times.
Loop from 1 to N instead of 0 to N-1
If you want the variable inside the loop to read 1 … N, start at 1 and stop at N + 1, because range excludes the stop bound:
n = 5
for k in range(1, n + 1):
print(k, end=" ")
print()That prints 1 2 3 4 5 . Using range(1, n) would stop at n - 1 and is a common off-by-one bug.
Repeat code N times using a while loop
Initialize a counter, test it each time, and increment manually. Easy to get wrong if you forget the update or the condition:
n = 4
count = 0
while count < n:
print("while", count)
count += 1That prints four lines with count equal to 0 through 3.
Repeat a string N times in Python
For text duplication you usually multiply a string instead of looping:
print("xo" * 4)That prints xoxoxoxo as a single new string.
Repeat values using itertools.repeat()
itertools.repeat(obj, times) is an iterator that yields obj exactly times times—handy when you want a for header but no real counter:
import itertools
for _ in itertools.repeat(None, 3):
print("again")That prints again on three lines. Omitting times would repeat forever, so always pass a count unless you break out early.
for loop vs while loop for repeating N times
| Situation | Prefer |
|---|---|
| Fixed number of iterations known before the loop | for _ in range(n) |
| Stop when a condition becomes true (unknown iterations) | while with a clear condition |
| Need index 0 … n-1 | for i in range(n) |
| Need index 1 … n | for i in range(1, n + 1) |
for plus range keeps increment and bound in one place, which reduces mistakes compared to hand-rolled while counters.
Common mistakes when looping N times
- Using
range(n)when you meantrange(1, n + 1)(or the reverse), which shifts every value by one. - Writing
range(1, n)when you need to includen—remember the stop value is excluded. - Mutating a
whilecounter inside the body in a way that skips or repeats iterations (for example forgettingcount += 1). - Using
for i in range(len(items))when you only need elements; preferfor item in itemsorenumeratewhen you need both index and value.
Python loop N times quick reference table
| Goal | Pattern |
|---|---|
Run n times, ignore index |
for _ in range(n): |
Indices 0 … n-1 |
for i in range(n): |
Indices 1 … n |
for i in range(1, n + 1): |
| Step by 2, start 0 | for i in range(0, n, 2): |
| Same body, iterator style | for _ in itertools.repeat(None, n): |
| Repeat string | s * n |
| Unknown stop condition | while cond: with careful updates |
Summary
To loop n times in Python, for _ in range(n) is the clearest default; use range(1, n + 1) when you want human-friendly numbering from 1 through n. while loops can repeat a fixed count but demand manual counter discipline. Strings duplicate with *; itertools.repeat pairs with for when you want a counted iterator without a numeric index. Match range stop values to whether the last value should be included, and reserve while for cases where the iteration count is not fixed in advance.
References
Frequently Asked Questions
What is the best way to loop N times in Python?
Use for _ in range(n) when you only need the body to run n times; use enumerate or a normal index name when you need the counter inside the loop.
Does range(10) run 10 times?
Yes: range(10) yields 0 through 9, which is ten iterations; range(1, 11) yields 1 through 10 if you want one-based labels.
How do I repeat a string N times?
Use the multiplication operator: "ab" * 3 produces "ababab"; that builds one new string rather than running a loop for side effects.
When should I use while instead of for range for counting?
Use while when the termination condition is not a simple fixed count ahead of time; for a known repeat count, for range is shorter and harder to get wrong.

