Draw with Loops: ASCII Art
Lesson 3 of 5 · View course roadmap
Learn the idea
Multiplying a string repeats it: "*" * 4 is "****". Combine that with a loop counter and you can draw:
range(1, 5)counts 1, 2, 3, 4 — a growing shaperange(3, 0, -1)counts 3, 2, 1 — a shrinking one (the third number is the step)print("*" * i)turns the count into a row
This is the gym where loops become intuition. When you can look at a shape and see the loop that draws it, iteration has clicked — and that's the exact skill behind rendering game boards, progress bars and terminal dashboards.
Where you'll use this
Progress bars, terminal dashboards, board-game grids and even the loading spinners in CLI tools are all 'draw with loops'. htop and git's progress output are professional ASCII art.
Common mistakes
- Off-by-one ranges: range(1, 5) gives four rows, not five — check your endpoints against the shape.
- Forgetting the third argument of range() for counting down: range(3, 0, -1), not range(3, 0).
- Building one giant string when a print-per-row loop is clearer and easier to debug.
Pro tip
Center shapes with str.center(): print(("*" * i).center(9)) turns your triangle into a pyramid — width math handled for you.
Try it yourself
The lesson example is loaded and ready — press Run, then change something and run it again. Breaking it is part of learning. Want a clean slate? Tap “New blank”.
Output appears here…
Pass the challenge +25 XP
Draw an arrow: a triangle growing from 1 to 4 stars, then shrinking from 3 back to 1 (each row on its own line).
* ** *** **** *** ** *
Two loops: range(1, 5) growing, then range(3, 0, -1) shrinking. Each prints "*" * i.
for i in range(1, 5):
print("*" * i)
for i in range(3, 0, -1):
print("*" * i)
Run your code to check it…
Check your understanding
Your notes (saved on this device)
Tip: use ← and → to move between lessons, ⌘K to search everything.