Step 01 Beginner Problems — Patterns
Pattern 2 — Right-Angled Triangle Pattern
A flight of stairs made of stars: the first row has 1 star and every row below it has one more, ending with n stars on the last row. This is the archetypal growing triangle — the first pattern where what a row contains depends on which row it is.
* * * * * * * * * * * * * * *
How to think about it
Number the rows from the top as i = 0, 1, ..., n-1. Row i contains exactly i + 1 stars and no leading spaces — that single formula, stars = i + 1, is the whole pattern. The outer loop walks i from 0 to n-1; for each row you print i + 1 star tokens joined by spaces. The last row (i = n - 1) has n stars, so the triangle's base is exactly as wide as the full n-by-n rectangle of Pattern 1, and the running total of stars is 1 + 2 + ... + n = n(n+1)/2.
Code
For each row index i the code creates a list of i + 1 '*' tokens and prints them joined by spaces. No indent is needed because the triangle is left-aligned — all the growth happens on the right edge.
def pattern(n):
for i in range(n):
print(" ".join(["*"] * (i + 1)))Key points
- Master formula: row i (0-based) prints i + 1 stars.
- Left-aligned patterns need no space loop — growth is purely in the token count.
- The base row always has exactly n stars; check your last line when debugging.
- Using 1-based rows instead? Then row r prints r stars — same thing, just shifted bookkeeping.
- This 'count grows with i' idea recurs in Patterns 3, 4, 10, 13, 14 and 16.