Increasing Odd Number Rows Pattern in Python

What You’ll Learn
How to print an increasing odd-length number pattern in Python: 1, 123, 12345, 1234567, 123456789.
You’ll use nested loops where the outer loop controls the row size (growing by 2 each time), and the inner loop prints numbers from 1 up to the row limit.
⭐ Pattern Output
For 5 rows, the pattern looks like this:
1
123
12345
1234567
123456789Complete Python Program
The outer loop increases by 2 each row (2, 4, 6, 8, 10). For each row, the inner loop prints numbers from 1 to i - 1, producing odd-length rows.
for i in range(2, 11, 2):
for j in range(1, i):
print(j, end="")
print()🧠 How It Works
Outer loop steps by 2
for i in range(2, 11, 2) generates 2, 4, 6, 8, 10. This controls how many digits appear on each row.
Inner loop prints 1..i-1
for j in range(1, i) prints numbers from 1 up to i - 1, giving 1, 3, 5, 7, 9 digits per row.
No spaces between digits
print(j, end="") keeps printing on the same line to form 12345 instead of 1 2 3 4 5.
Move to the next line
print() adds a newline after each row.
Increasing odd-length rows
Total digits printed in \(n\) rows is 1+3+5+...+(2n-1) = n², so time complexity is O(n²).
Variation — User Input Version
Let the user choose the number of rows. Each next row grows by 2 digits automatically.
rows = int(input("Enter the number of rows: "))
for i in range(2, 2 * rows + 1, 2):
for j in range(1, i):
print(j, end="")
print()💡 Tips for Enhancement
Try These
- Validate input (reject
rows < 1) before printing - Add spaces between numbers with
print(j, end=" ") - Start from a different number (like 0 or 5) to create variations
- Right-align the pattern by printing leading spaces before each row
- Replace digits with characters to build alphabet patterns
Avoid
- Forgetting
print()after each row - Using
range(1, i + 1)(it changes the row length to even) - Assuming user input is always valid (wrap
int()conversion if needed) - Mixing spacing/formatting rules without updating the output section
Key Takeaways
The pattern grows by odd lengths: 1, 3, 5, 7, 9 digits.
Using range(2, ..., 2) is a clean way to control row width in steps of 2.
Total digits printed for \(n\) rows is \(n^2\).
The same nested-loop approach applies to star patterns and alphabet patterns.
❓ Frequently Asked Questions
1 to i - 1 and i increases by 2, so the row lengths become 1, 3, 5, 7, 9.rows, then loop i from 2 to 2 * rows (inclusive) in steps of 2. Each row prints from 1 to i - 1.print(j, end=" "). You may want to strip() the trailing space when displaying output.Explore More Python Number Patterns!
From pyramids to Floyd’s triangle and beyond—practice nested loops with progressively richer patterns.
The total printed digits across \(n\) rows is a perfect square: 1+3+5+...+(2n-1) = n². For \(n = 5\), you print 25 digits in total.
12 people found this page helpful
