Free lab Real Python 3. Zero installs. Your code stays in this browser. Open the playground

Draw with Loops: ASCII Art

10 min 25 XP
Study loop Read Predict Run Tweak Prove Review
Lesson 3 of 5 · View course roadmap
Step 1

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 shape
  • range(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, with the width math handled for you.

Step 2

Try it yourself

Blank · autosaved

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”.

PYexample.py
⌘ + Enter to run
Output appears here…
Step 3

Pass the challenge +25 XP

Blank · autosaved

Draw an arrow: a triangle growing from 1 to 4 stars, then shrinking from 3 back to 1 (each row on its own line).

Target output
*
**
***
****
***
**
*
PYchallenge.py
Run your code to check it…
Step 4

Check your understanding

1. What does "ha" * 3 produce?
2. What does range(3, 0, -1) generate?
Last step

Your notes (saved on this device)

Tip: use ← and → to move between lessons, ⌘K to search everything.

Your next ten minutes

Write Python that does something useful.

Start free. No install, no card, no passive video marathon.

Start learning free → Explore the path