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 triangle of stars, aligned to the right, where each row contains one more star than the previous.


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 starts with spaces to align the triangle to the right
- Then i stars are printed with a space between each
- The result is a clean, right-aligned triangle of stars


Written & researched by Dr. Shahin Siami