Increasing Odd Number Rows Pattern in Python

Beginner
⏱️ 5 min read
📚 Updated: Aug 2025
🎯 2 Code Examples
Nested Loops

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:

Output
1
123
12345
1234567
123456789
1

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

Python
for i in range(2, 11, 2):
    for j in range(1, i):
        print(j, end="")
    print()

🧠 How It Works

1

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.

Row size
2

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.

Number printing
3

No spaces between digits

print(j, end="") keeps printing on the same line to form 12345 instead of 1 2 3 4 5.

Formatting
4

Move to the next line

print() adds a newline after each row.

Line break
=

Increasing odd-length rows

Total digits printed in \(n\) rows is 1+3+5+...+(2n-1) = n², so time complexity is O(n²).

2

Variation — User Input Version

Let the user choose the number of rows. Each next row grows by 2 digits automatically.

Python
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

1

The pattern grows by odd lengths: 1, 3, 5, 7, 9 digits.

2

Using range(2, ..., 2) is a clean way to control row width in steps of 2.

3

Total digits printed for \(n\) rows is \(n^2\).

4

The same nested-loop approach applies to star patterns and alphabet patterns.

❓ Frequently Asked Questions

Each row prints digits from 1 to i - 1 and i increases by 2, so the row lengths become 1, 3, 5, 7, 9.
Yes. Ask for rows, then loop i from 2 to 2 * rows (inclusive) in steps of 2. Each row prints from 1 to i - 1.
Change the print statement to print(j, end=" "). You may want to strip() the trailing space when displaying output.
O(n²) for n rows, since total digits printed are 1+3+...+(2n-1) = n².

Explore More Python Number Patterns!

From pyramids to Floyd’s triangle and beyond—practice nested loops with progressively richer patterns.

All Number Patterns →
Did you know?

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.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

12 people found this page helpful