Part of the series

Several example codes

~1 min read • Updated Oct 7, 2025

Program Overview

This Python program reads the number of rows from the user.
It then prints a pyramid of stars, aligned to the right, where each row contains one more star than the previous.
The spacing ensures the triangle shape is visually balanced.


Python Code:


rows = int(input("Enter number of rows: "))

for i in range(1, rows + 1):
    print(" " * (rows - i), end="")  # spacing for alignment
    print("* " * i)

Sample Output for 5 Rows:


    *
   * *
  * * *
 * * * *
* * * * *

Step-by-Step Explanation:

- The outer loop runs from 1 to rows
- Each row begins with spaces to align the stars to the right
- Then i stars are printed with a space between each
- The result is a clean, right-aligned pyramid of stars


Written & researched by Dr. Shahin Siami