Star pattern programs print * and spaces in rows to form triangles, pyramids, diamonds, and hollow shapes. They are the classic first step into nested for loops in Python.
Remember
Starter rule (Program 1): on row i, print i stars
*
**
***
****
***** ← 5 rows
Learn the right-angled triangle first, then reuse the same outer-row idea for inverted shapes, leading spaces, and mirrored halves. This hub teaches that foundation, then links to all 11 tutorials.
Approach
How to Solve It
Start with nested loops for the starter triangle, 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
Spaces + stars
Add a space loop before the star loop
Right-aligned / centered shapes (Programs 3–6)
Pseudocode
Pseudocode
for i from 1 to rows:
for j from 1 to i:
print "*" (no newline)
print newline
Same idea as C# Write / WriteLine: print characters without a newline, then end the row once.
Try it
Live Preview
Change the row count and the starter 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 starter programs for the right-angled triangle: fixed rows, input(), and "*" * i. Use View Output to reveal sample results. Full deep-dives live in each program page.
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.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 any star-pattern 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.
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.
j to rows
Rectangle, not triangle
Inner bound must be j <= i (or 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.
rows = 1
Single star
Output is just * on one line — a good sanity check for any pattern.
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
Typical series (Programs 1–11)
O(rows²)
O(1) for loop versions
For the starter triangle, total stars = n(n+1)/2. Wider or two-half shapes still print a quadratic number of characters in n.
They print asterisks and spaces in rows to form triangles, pyramids, diamonds, and hollow shapes. Nested for loops plus print("*", end="") and print() are the usual tools.
Start with Program 1 (right-angled triangle). It teaches the outer-row / inner-star idea every later pattern builds on.
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.
print("*", end="") stays on the same line. print() ends the current line. Stars use end=""; the row break uses print() after the inner loop.
Yes. print("*" * i) prints a full row in one call. Nested loops are better for learning; string multiplication is a handy shortcut later.
Most print O(n²) characters for n rows. The starter triangle prints n(n+1)/2 stars; wider or two-half shapes stay quadratic in n.
Wrap int(input()) in try/except ValueError, or check that the raw string is digits, and require rows >= 1.
Add a space loop before the star loop for centering, then mirror the upper half for inverted or diamond shapes. See Programs 5–11.
🤔
Did you know?
Almost every star pattern is the same idea: an outer loop picks the row, then inner loops decide spaces and stars. Master the right-angled triangle first — the rest reuse that skeleton.