Python Number Rows Pattern (Odd Length)

Beginner
5 min read
Updated: Sep 2026
3 programs
Live preview

What Is This Pattern?

Increasing odd-length number rows print consecutive digits 1..i with no spaces, where row lengths grow by 2: 1, 3, 5, 7, 9.

Remember
Rule: for odd i = 1, 3, 5, … max_n
      print digits 1..i (no spaces)

1
123
12345
1234567
123456789     ← max_n = 9

The outer loop’s range(..., 2) is the whole trick — a clear follow-up after the jump triangle in Program 21.

How to Solve It

Walk odd lengths with range(..., 2); on each row print j from 1 to i with end="".

MethodIdeaBest for
Outer step 2range(..., 2); inner 1..iLearning, interviews
input() maxSame loops; force odd max if neededInteractive practice
Odd-only digitsInner loop also uses step 2Variant practice

Pseudocode

Pseudocode
for i from 1 to max_n step 2:
    for j from 1 to i:
        print j (same line, no spaces)
    print newline

Cheat sheet

GoalPattern
Set maxmax_n = 9
Outer loopfor i in range(1, max_n + 1, 2):
Inner loopfor j in range(1, i + 1):
Print digitprint(j, end="")
End rowprint()
Force odd maxif max_n % 2 == 0: max_n -= 1
Odd digits onlyfor j in range(1, i + 1, 2):

Printing Numbers vs Starting a New Line

APIEffectUse for
print(j, end="")Stays on the same line, no spaceEach digit
print()Ends the current lineAfter the inner loop

Glue digits with end="", then break once with print(). Using end=" " adds spaces; putting print() inside the inner loop prints one digit per line.

Live Preview

Change the maximum odd length and the pattern updates instantly. Even values are adjusted down by 1.

Whole numbers from 1 to 15. Even inputs become the next lower odd value.

Live result max = 9 · digits = 25
1
123
12345
1234567
123456789

Worked Walkthrough — max_n = 5

Trace each odd value of i and the digits concatenated on that row.

iInner jPrinted row
111
31 2 3123
51 2 3 4 512345

Next odd i would be 7 — row length grows by exactly 2 each time.

Python Programs

Three complete programs: fixed max_n = 9, input() max, and odd-only digits. Use View Output for sample results.

Example 1 — Fixed max_n = 9

Hard-coded maximum — outer step 2, inner concatenates 1..i.

Python
max_n = 9

for i in range(1, max_n + 1, 2):
    for j in range(1, i + 1):
        print(j, end="")
    print()

How It Works

1. Outer step. i runs 1, 3, 5, 7, 9 — those are the row lengths.

2. Inner digits. Print j from 1 to i with end="" so digits glue together.

3. End the row. print() moves to the next line after the digit string is done.

Example 2 — input() Max

Read the maximum at runtime; if it is even, subtract 1 so the last row stays odd-length.

Python
max_n = int(input("Enter the maximum value: "))

if max_n % 2 == 0:
    max_n -= 1

for i in range(1, max_n + 1, 2):
    for j in range(1, i + 1):
        print(j, end="")
    print()

How It Works

1. Read max. Convert the prompt answer with int().

2. Oddify if needed. If even, max_n -= 1 keeps an odd last length.

3. Safer input tip. Bare int(input()) raises ValueError on letters. Prefer:

Safer input
raw = input("Enter the maximum value: ").strip()
try:
    max_n = int(raw)
except ValueError:
    print("Enter a positive whole number.")
    raise SystemExit(1)
if max_n < 1:
    print("Enter a positive whole number.")
    raise SystemExit(1)

Example 3 — Odd-Only Digits range(..., 2)

Keep max_n = 9 but print only odd digits on each row.

Python
max_n = 9

for i in range(1, max_n + 1, 2):
    for j in range(1, i + 1, 2):
        print(j, end="")
    print()

How It Works

1. Same outer loop. Row lengths are still 1, 3, 5, 7, 9.

2. Skip evens. Inner range(..., 2) prints only 1, 3, 5, … up to i.

3. Compare shapes. Rows are shorter than Example 1 because half the digits are skipped.

Edge Cases & Pitfalls

Check these before calling the solution done.

no step

Forgot step 2

Using range(1, max_n + 1) prints every length 1, 2, 3, … — not only odd lengths. Keep step 2.

spaces

Extra spaces

Use end="", not end=" ", if you want tight digit strings like 12345.

print() inside

Vertical digits

If print() is inside the inner loop, each digit lands on its own line.

even max

Even maximum

With max_n = 8, the last odd i is 7 — or call max_n -= 1 first so intent is explicit.

max_n = 1

Single digit

Output is just 1 on one line.

input()

Catch ValueError

Letters crash bare int(input()) — wrap in try/except and require max_n >= 1.

Time and Space Complexity

ProgramTimeExtra space
Examples 1–2O(n²)O(1)
Example 3O(n²)O(1) (fewer digits, same order)

Total digits for odd lengths = 1 + 3 + 5 + … + n ≈ (n/2)² → still O(n²).

Key Takeaways

  • Rule: for each odd i, glue digits 1..i with end="".
  • Step 2: range(..., 2) makes lengths 1, 3, 5, 7, 9.
  • end= vs print(): digits stay on the line; print() advances after each row.
  • Complexity: O(n²) for maximum length n.

One line: step the outer loop by 2, and glue digits 1 through i together on each row.

Frequently Asked Questions

Because the outer loop uses range with step 2, so i becomes 1, 3, 5, 7, 9 — each row prints one more odd count of digits than the previous.
print(j, end="") prints each digit immediately after the previous one on the same line — no space character is added.
Yes. Change the inner loop to range(1, i + 1, 2) to output 1, 13, 135, 1357, 13579 (see Example 3).
print(j, end="") stays on the same line with no trailing space. print() ends the current line. Digits use end=""; the row break uses print() after the inner loop.
Because the outer loop runs while i <= 9 (range(1, 10, 2)). Change 9 to any odd maximum to extend the pattern.
Subtract 1 to make it odd (see Example 2) so the last row still has an odd length.
O(n²) for maximum row length n because total prints are 1+3+5+…+n.
Use try/except ValueError around int(input()) so bad input does not crash the script.
The outer loop never runs, so nothing is printed. Validate and prompt again if you want a clear user message.

Did you know?

Each row length increases by 2 because the outer loop uses range(..., 2) (1, 3, 5, 7, 9). The inner loop prints 1..i with end="" — total prints grow as O(n²) for maximum row length n.

Next: Number & Asterisk Mirror

Continue with the next pattern in the Python number-pattern series.

Program 23 tutorial →

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