Python Loop N Times: Repeat Code Using range()

Learn how to loop N times in Python using for range(), repeat code 5 or 10 times, loop without an index, use while loops, and avoid common off-by-one mistakes.

Published

Updated

Read time 5 min read

Reviewed byDeepak Prasad

Python Loop N Times: Repeat Code Using range()

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:

python
n = 5
for _ in range(n):
    print("tick")
Output

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).

python
for i in range(4):
    print(i, end=" ")
print()
Output

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:

python
for i in range(10):
    print(i, end=" ")
print()
Output

That prints the digits 0 through 9 separated by spaces.


Loop 5 times or 3 times in Python

Change only the argument to range:

python
for _ in range(5):
    print("five")

for _ in range(3):
    print("three")
Output

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:

python
for _ in range(3):
    print("no index")
Output

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 1N, start at 1 and stop at N + 1, because range excludes the stop bound:

python
n = 5
for k in range(1, n + 1):
    print(k, end=" ")
print()
Output

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:

python
n = 4
count = 0
while count < n:
    print("while", count)
    count += 1
Output

That 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:

python
print("xo" * 4)
Output

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:

python
import itertools

for _ in itertools.repeat(None, 3):
    print("again")
Output

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 meant range(1, n + 1) (or the reverse), which shifts every value by one.
  • Writing range(1, n) when you need to include n—remember the stop value is excluded.
  • Mutating a while counter inside the body in a way that skips or repeats iterations (for example forgetting count += 1).
  • Using for i in range(len(items)) when you only need elements; prefer for item in items or enumerate when 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

1. 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.

2. 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.

3. 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.

4. 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.
Deepak Prasad

R&D Engineer

Founder of GoLinuxCloud with more than 15 years of expertise in Linux, Python, Go, Laravel, DevOps, Kubernetes, Git, Shell scripting, OpenShift, AWS, Networking, and Security. With extensive …