Python Ascending Number Triangle Pattern

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

What Is This Pattern?

An ascending number triangle grows one digit per row: row i prints digits 1 through i.

Remember
Rule: on row i, print j for j = 1..i

1
12
123
1234
12345   ← rows = 5

Follows the left-aligned descending triangle in Program 4; next is the reverse ascending triangle in Program 6.

How to Solve It

Outer loop picks row i from 1 to rows. Inner loop prints digits 1..i.

MethodIdeaBest for
Tight digitsprint(j, end="") with no spacesLearning, interviews, exams
Spaced digitsprint(j, end=" ")Easier reading / wider values

Pseudocode

Pseudocode
for i from 1 to rows:
    for j from 1 to i:
        print j (no newline)
    print newline

Cheat sheet

GoalPattern
Walk rowsfor i in range(1, rows + 1):
Walk digitsfor j in range(1, i + 1):
Print digitprint(j, end="")
Spaced digitprint(j, end=" ")
End the rowprint()

Printing Numbers vs Starting a New Line

APIEffectUse for
print(j, end="")Stays on the same lineEach digit
print()Ends the current lineAfter the inner loop finishes a row

Print digits without a newline, then end the row once. Putting bare print() inside the inner loop turns the triangle into a vertical list.

Live Preview

Change the row count and the ascending triangle updates instantly.

Whole numbers from 1 to 9 (single digits stay tidy). Tap a chip or type a value — the preview redraws as you go.

Live result 5 rows · 15 digits
1
12
123
1234
12345

Worked Walkthrough — row i = 4

Trace four digits glued together as 1234.

jPrintLine so far
111
2212
33123
441234

After j finishes, bare print() starts the next row. Row 5 will print one more digit — 12345.

Python Programs

Three complete programs: fixed rows = 5, input() rows, and spaced digits. Use View Output to reveal sample results.

Example 1 — Fixed rows = 5

Hard-coded height — outer loop grows the row, inner loop prints 1..i.

Python
rows = 5

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

How It Works

1. Outer loop grows. i runs from 1 to 5 — each value is one row.

2. Inner loop prints 1..i. j runs from 1 to i; each digit uses print(j, end="").

3. Newline. Bare print() runs only after the inner loop finishes.

Example 2 — User Input Rows

Read the row count with input() and build the same triangle.

Python
try:
    rows = int(input("Enter the number of rows: "))
except ValueError:
    print("Please enter a positive integer.")
    raise SystemExit(1)

if rows < 1:
    print("Please enter a positive integer.")
    raise SystemExit(1)

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

How It Works

1. Validate rows. Catch ValueError; require rows >= 1.

2. Same triangle. Only the bound changes from the literal 5 to the user value.

3. Safer input tip. Cap demos for readable output:

Safer input tip
if rows < 1 or rows > 9:
    print("Enter a whole number from 1 to 9.")
    raise SystemExit(1)

Example 3 — Spaced Digits

Same loops — add a space after each digit for easier reading.

Python
rows = 5

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

How It Works

1. Only the print changes. print(j, end=" ") instead of print(j, end="").

2. Same bounds. Outer and inner limits match Example 1 exactly.

3. Useful for wider values. Spaces keep multi-digit numbers readable when rows grows.

Edge Cases & Pitfalls

Check these before calling the solution done.

print() inside

print() inside the inner loop

That prints one digit per line. Call bare print() only after the inner loop.

no end

Forgot end=""

Default print(j) puts each digit on its own line. Use end="" (or end=" ") inside the row.

no newline

Missing bare print()

Without it, every digit glues onto one endless line.

outer down

Outer loop counts down

You get Program 1’s shrinking triangle (12345, 1234, …). Keep i from 1 up to rows.

rows = 1

Single row

Output is just 1. A good sanity check for input validation.

input()

Catch ValueError

Bare int(input()) crashes on non-numeric text — wrap it in try/except ValueError.

Time and Space Complexity

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

Total prints are 1 + 2 + … + n = n(n + 1) / 2 — quadratic in n.

Key Takeaways

  • Rule: on row i, print j for j = 1..i.
  • Grow: outer loop counts up so each row adds one digit.
  • Break the row: call bare print() only after the inner loop.
  • Complexity: O(n²) time; O(1) extra space.

One line: for each row i from 1 to rows, print digits 1..i, then end the line.

Frequently Asked Questions

The outer loop runs i from 1 to rows. For each row i, the inner loop runs j from 1 to i and prints j. Row 1 prints 1, row 2 prints 12, and so on until row rows prints 1..rows.
Because the outer loop counts up and the inner bound equals i. When i increases, each row prints one more digit than the previous row.
Program 4 prints rows..i in descending order (54321, 5432, …). Program 5 prints 1..i in ascending order — a growing triangle instead of a shrinking one.
Program 1 counts the outer loop down and prints 12345, 1234, … Program 5 counts up and prints 1, 12, 123, … — same inner loop, opposite outer direction.
Printing a digit with end="" stays on the same line. Bare print() ends the current row. Digits use end=""; the row break uses print() after the inner loop.
O(n²) for n rows because total prints are 1 + 2 + … + n = n(n+1)/2.
Use print(j, end=" ") instead of print(j, end="") — see Example 3.
Use try/except ValueError around int(input()) and require rows >= 1 — see Example 2.

Did you know?

Row i prints digits 1 through i. The outer loop counts up from 1 to rows, so each row grows by one digit — total prints = n(n+1)/2, time O(n²).

Next: Reverse Ascending Number Triangle

Continue with a related ascending pattern that reverses each row.

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