Python Practice › Loop Programs › Program 44
Star pyramid
Write a Python program to print a pyramid of stars with 5 rows (1, 3, 5, 7, 9 stars).
Output
* *** ***** ******* *********
Approach
- Row i has
2i - 1stars andn - ileading spaces.
Solution
n = 5for i in range(1, n + 1): print(" " * (n - i) + "*" * (2 * i - 1))Output
* *** ***** ******* *********
Line by line
Line 3
Fewer spaces and more stars on each row make the pyramid shape.
Learn the concept
New to this topic? Read the lesson, then come back.