Python Number Triangle Pattern (Fill with N)

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

What Is This Pattern?

A fill-with-n triangle prints a fixed-width row each time — first the ascending sequence i..n, then pad the rest with n.

Remember
Rule: for i = n..1
      print i..n, then pad with n (i - 1 times)

5 5 5 5 5
4 5 5 5 5
3 4 5 5 5
2 3 4 5 5
1 2 3 4 5     ← n = 5

After Program 18’s odd/even triangle, this pattern teaches two inner loops that keep every row the same width.

How to Solve It

Walk i from n down to 1; print the sequence, then pad with n.

MethodIdeaBest for
Two inner loopsSequence i..n; fill i - 1 times with nLearning, interviews
input() widthSame loops; read n at runtimeInteractive practice
Custom fillSame shape; pad with a separate constantFill variants

Pseudocode

Pseudocode
for i from n down to 1:
    for j from i to n:
        print j (same line, spaced)
    for j from 1 to i - 1:
        print n (same line, spaced)
    print newline

Cheat sheet

GoalPattern
Set widthn = 5
Outer loopfor i in range(n, 0, -1):
Sequencefor j in range(i, n + 1): print(j, end=" ")
Fill padfor j in range(1, i): print(n, end=" ")
End rowprint()
Width per rowAlways n
Custom fillprint(fill, end=" ") in the second loop

Printing Numbers vs Starting a New Line

APIEffectUse for
print(j, end=" ")Stays on the same lineEach number
print()Ends the current lineAfter both inner loops

Print numbers with end=" ", then break once with print(). Putting print() inside an inner loop prints one number per line.

Live Preview

Change n and the fill-with-n triangle updates instantly.

Whole numbers from 1 to 9. Tap a chip or type a value — the preview redraws as you go.

Live result n = 5 · values = 25
5 5 5 5 5 
4 5 5 5 5 
3 4 5 5 5 
2 3 4 5 5 
1 2 3 4 5 

Worked Walkthrough — n = 3

Trace each outer i, the sequence segment, the fill count, and the printed row.

iSequence i..nFill countPrinted row
332 × 33 3 3
22 31 × 32 3 3
11 2 3none1 2 3

Fill count = i - 1. Every row has exactly n values. Total = n².

Python Programs

Three complete programs: fixed n = 5, input() width, and custom fill. Use View Output for sample results.

Example 1 — Fixed n = 5

Hard-coded width — sequence loop, then pad with n.

Python
n = 5

for i in range(n, 0, -1):
    for j in range(i, n + 1):
        print(j, end=" ")

    for j in range(1, i):
        print(n, end=" ")

    print()

How It Works

1. Outer loop. i runs from 5 down to 1 — the start of the ascending sequence.

2. Sequence. Print j from i to n.

3. Fill. Print n exactly i - 1 times, then print() ends the row.

Example 2 — input() Width

Read n at runtime; fill value matches the width.

Python
n = int(input("Enter the triangle width: "))

for i in range(n, 0, -1):
    for j in range(i, n + 1):
        print(j, end=" ")

    for j in range(1, i):
        print(n, end=" ")

    print()

How It Works

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

2. Same shape. Sequence and fill still use the same n — only the source changes.

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

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

Example 3 — Custom Fill

Pad with fill = 9 while the sequence still runs up to n = 5.

Python
n = 5
fill = 9

for i in range(n, 0, -1):
    for j in range(i, n + 1):
        print(j, end=" ")

    for j in range(1, i):
        print(fill, end=" ")

    print()

How It Works

1. Same structure. Outer and both inner loop bounds match Example 1.

2. Only the pad changes. The second loop prints fill instead of n.

3. Sequence unchanged. The ascending i..n part still uses the width n.

Edge Cases & Pitfalls

Check these before calling the solution done.

missing fill

Uneven rows

Without the second loop, early rows are short. Always pad i - 1 times.

print() inside

Vertical numbers

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

ascending outer

Upside-down order

Using range(1, n + 1) for the outer loop prints the full sequence first. Keep range(n, 0, -1) for this shape.

range(1, i + 1)

One extra pad

Fill with range(1, i) — that is i - 1 times. range(1, i + 1) overshoots the width.

n = 1

Single value

Output is just 1 — the fill loop never runs.

input()

Catch ValueError

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

Time and Space Complexity

ProgramTimeExtra space
Examples 1–3O(n²)O(1)

Each of n rows prints n values → total n² → O(n²).

Key Takeaways

  • Rule: print i..n, then pad with n for i - 1 slots.
  • Fixed width: every row has exactly n values.
  • end= vs print(): numbers stay on the line; print() advances after both loops.
  • Complexity: total values = n² → O(n²).

One line: print the rising tail, then fill the rest of the row with n.

Frequently Asked Questions

When i = 5, the sequence loop prints j = 5 once, then the fill loop runs 4 times — all values are 5, so the row is 5 5 5 5 5.
The first prints the increasing sequence i..n. The second fills remaining positions with n so every row has the same width.
When i = 1, the sequence loop prints j = 1 to 5 and the fill loop runs zero times — no padding needed.
print(j, end=" ") stays on the same line with a trailing space. print() ends the current line. Numbers use end=" "; the row break uses print() after both inner loops.
Replace the hard-coded n in the fill loop with any constant — see Example 3 with fill = 9.
Rows will have different widths — the top row may be short while the bottom row is full length.
O(n²) for width n because each of n rows prints n numbers.
Use try/except ValueError around int(input()) so bad input does not crash the script.
Only one row prints — a single 1.

Did you know?

Each row prints an ascending sequence i..n, then pads with n so every row has width n. The second inner loop runs i - 1 times — still O(n²) total prints.

Next: Continuous Number Triangle

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

Program 20 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