Part of the series

Several example codes

~1 min read • Updated Oct 6, 2025

Program Overview

This Python program reads an integer n between 1 and 80 and prints a centered triangle using asterisks (*).
Each row contains one more asterisk than the previous, and the pattern is padded with spaces to align the triangle symmetrically.


Python Code:


# Read n from user
n = int(input("Enter a number between 1 and 80: "))

# Validate input
if 1 <= n <= 80:
    for i in range(1, n + 1):
        spaces = ' ' * (n - i)
        stars = '* ' * i
        print(spaces + stars.strip())
else:
    print("The number must be between 1 and 80.")

Sample Output for n = 5:


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

Step-by-Step Explanation:

- The user inputs a number n
- For each row i from 1 to n:
— The program prints n - i spaces to center the row
— Then it prints i asterisks separated by spaces
— The .strip() method removes trailing space for clean alignment


Written & researched by Dr. Shahin Siami