Python Program to Print a Star Pattern
Beginner · Patterns · CBSE class 11 practical list
Pattern programs print shapes made of characters, one row at a time. This one prints a right-angled triangle of stars, where row 1 has one star, row 2 has two, and so on. It is a classic exercise for practising nested loops.
Program
* ** *** **** *****
How it works
- The outer loop runs once for each row, with
igoing from 1 torows. - The inner loop prints
istars on that row, andend=""keeps them on the same line. - The empty
print()after the inner loop moves the cursor to the next line. - Change
rowsto make the triangle taller or shorter.
Other ways to write it
Pyramid pattern
* *** ***** ******* *********
Each row starts with rows - i spaces to centre it, then has 2 * i - 1 stars, so the rows have 1, 3, 5, 7 and 9 stars.
Inverted triangle
***** **** *** ** *
Counting down with range(rows, 0, -1) prints the longest row first, and "*" * i repeats the star i times without an inner loop.
Try it yourself
Change the program so the right angle is on the right side, by printing rows - i spaces before the stars on each row.
Edit the program above and press Run, or open it in the full editor to save it and share it. To watch it run line by line, open it in the visualizer.
Related programs
All Python programs · CBSE class 12 practical file · Python exercises