Python Practice › Loop Programs › Program 44

Star pyramid

EasyLoop Programs · 44 of 90

Write a Python program to print a pyramid of stars with 5 rows (1, 3, 5, 7, 9 stars).

Advertisement
Output
    *
   ***
  *****
 *******
*********

Approach

  1. Row i has 2i - 1 stars and n - i leading spaces.

Solution

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

More loop programs

← All 90 Loop Programs

Leave a Comment