A right-angled triangle star pattern prints a left-aligned staircase of * characters: row i has exactly i stars.
Remember
Rule: on row i, print i stars
*
**
***
****
***** ← 5 rows
In Python you solve it with two nested for loops: the outer loop picks the row, the inner loop prints stars on that row, then bare print() moves to the next line. Once this clicks, inverted triangles, pyramids, and hollow shapes become much easier.
Approach
How to Solve It
Two ways to emit the same shape — start with nested loops, then optionally shorten with string multiplication.
Method
Idea
Best for
Nested loops
Outer = rows, inner = stars via print(..., end="")
Learning, interviews, exams
"*" * i
Build a whole row in one call
Shorter demos once loops click
Pseudocode
Pseudocode
for i from 1 to rows:
for j from 1 to i:
print "*" (no newline)
print newline
Print stars without a newline, then end the row once.
Try it
Live Preview
Change the row count and the triangle updates instantly — including the triangular star total.
Whole numbers from 1 to 20. Tap a chip or type a value — the preview redraws as you go.
Live result5 rows · 15 stars
*
**
***
****
*****
Trace
Worked Walkthrough — rows = 4
Trace each outer-loop value of i and count how many times the inner loop runs.
i
Inner j
Printed row
Stars
1
1..1
*
1
2
1..2
**
2
3
1..3
***
3
4
1..4
****
4
Total star prints: 1 + 2 + 3 + 4 = 10 = 4×5/2. That triangular sum is why time is O(n²).
Code
Python Programs
Three complete programs: fixed rows, input(), and a "*" * i shortcut. Use View Output to reveal sample results.
Example 1 — Fixed rows = 5
Hard-coded height — ideal for first demos and screenshots.
Python
rows = 5
for i in range(1, rows + 1):
for j in range(1, i + 1):
print("*", end="")
print()
Output
*
**
***
****
*****
How It Works
1. Set height.rows = 5 means the triangle has five lines.
2. Outer loop picks the row.i runs from 1 to rows via range(1, rows + 1).
3. Inner loop prints stars. For each i, j runs from 1 to i, so row i gets exactly i stars via print("*", end="").
4. Break the line. Bare print() after the inner loop starts the next row.
When i = 1 you get *; when i = 2 you get **; and so on up to five stars.
Example 2 — User Input Version
Read the row count at runtime. Prefer a try / except in real apps (shown in the tip below).
Python
rows = int(input("Enter the number of rows: "))
for i in range(1, rows + 1):
for j in range(1, i + 1):
print("*", end="")
print()
Output (when user enters 7)
Enter the number of rows: 7
*
**
***
****
*****
******
*******
How It Works
1. Prompt and read.input() returns a string; int(...) converts it to a whole number.
2. Same nested-loop core. Only the source of rows changes — the print logic matches Example 1.
3. Safer input tip.int(input()) raises ValueError on letters. Prefer:
Safer input
raw = input("Enter the number of rows: ").strip()
try:
rows = int(raw)
except ValueError:
print("Enter a positive whole number.")
raise SystemExit
if rows < 1:
print("Enter a positive whole number.")
raise SystemExit
Example 3 — "*" * i
Build each row in one call — same shape, no explicit inner star loop.
Python
rows = 5
for i in range(1, rows + 1):
print("*" * i)
Output
*
**
***
****
*****
How It Works
1. One outer loop. Still walk i from 1 to rows.
2. Build the row."*" * i creates a string of length i filled with stars.
3. Print and advance.print(...) prints that string and ends the line.
Learn the two-loop version first (Examples 1–2) so you can explain both bounds in an interview; treat this as a polish shortcut afterward.
Edge Cases & Pitfalls
Check these before calling the solution done.
print() inside
Column of stars
If bare print("*") is inside the inner loop, each star lands on its own line. Use print("*", end="") for stars; print() only after the inner loop.
j to rows
Rectangle, not triangle
Inner bound must be range(1, i + 1). Looping to rows every time prints a filled rectangle.
No print()
One endless line
Omitting the row break glues every star onto a single line.
range off-by-one
Wrong height
Use range(1, rows + 1) for 1..rows. range(rows) alone gives 0..rows-1 and usually breaks the star count.
rows = 1
Single star
Output is just * on one line — a good sanity check.
rows ≤ 0
Empty output
Outer loop never runs. Validate and re-prompt for interactive programs.
Bad input
Catch ValueError
int(input()) throws on letters — prefer try / except ValueError.
Analysis
Time and Space Complexity
Program
Time
Extra space
Nested loops (Examples 1–2)
O(rows²)
O(1)
"*" * i (Example 3)
O(rows²)
O(rows) per temporary row string
Total stars printed = 1 + 2 + … + n = n(n+1)/2, which is still quadratic in n.
Remember
Key Takeaways
Rule: row i prints exactly i stars.
Two loops: outer = rows, inner = stars with print("*", end="").
Break the row: call bare print() only after the inner loop.
Complexity:O(n²) time from the triangular star count; O(1) extra space for nested loops.
One line: for each row i, print i stars with end="", then call print().
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 a star. Row 1 prints 1 star, row 2 prints 2 stars, and so on.
You need one loop for which row you are on and another for how many characters belong on that row. Nested for loops express that directly.
print("*", end="") stays on the same line. print() ends the current line. Stars use end=""; the row break uses print() after the inner loop.
Reverse the outer loop so i runs from rows down to 1, for example for i in range(rows, 0, -1). The first line then has rows stars. See Program 2.
O(n²) where n is the number of rows. Total star prints equal 1+2+…+n = n(n+1)/2.
Yes. print("*" * i) prints a full row in one call. Nested loops are better for learning; "*" * i is a handy shortcut later.
Wrap int(input()) in try/except ValueError, or check that the raw string is digits, and require rows >= 1.
The outer loop never runs, so nothing is printed. Validate and prompt again if you want a clear user message.
🤔
Did you know?
Row i prints exactly i stars. Total stars for n rows is the triangular number n(n+1)/2 — the same count that makes this pattern O(n²).