0
0
PythonProgramBeginner · 2 min read

Python Program to Print Pyramid Star Pattern

You can print a pyramid star pattern in Python using nested loops like for i in range(1, n+1): print(' '*(n-i) + '*'*(2*i-1)) where n is the number of pyramid rows.
📋

Examples

Input3
Output * *** *****
Input5
Output * *** ***** ******* *********
Input1
Output*
🧠

How to Think About It

To print a pyramid star pattern, think of each row as having spaces followed by stars. The number of spaces decreases each row, and the number of stars increases by two. Use one loop to go through each row and inside it print spaces first, then stars.
📐

Algorithm

1
Get the number of rows (n) from the user.
2
For each row from 1 to n:
3
Calculate and print spaces equal to (n - current row).
4
Calculate and print stars equal to (2 * current row - 1).
5
Move to the next line after printing each row.
💻

Code

python
n = int(input('Enter number of rows: '))
for i in range(1, n + 1):
    print(' ' * (n - i) + '*' * (2 * i - 1))
Output
Enter number of rows: 5 * *** ***** ******* *********
🔍

Dry Run

Let's trace the program for input n=3 to see how it prints the pyramid.

1

Input

User enters n = 3

2

First row (i=1)

Print spaces: 3 - 1 = 2 spaces, Print stars: 2*1 - 1 = 1 star Output: ' *'

3

Second row (i=2)

Print spaces: 3 - 2 = 1 space, Print stars: 2*2 - 1 = 3 stars Output: ' ***'

4

Third row (i=3)

Print spaces: 3 - 3 = 0 spaces, Print stars: 2*3 - 1 = 5 stars Output: '*****'

Row (i)SpacesStarsPrinted Line
121 *
213 ***
305*****
💡

Why This Works

Step 1: Spaces decrease each row

We print spaces first to push stars to the right, using n - i spaces for the current row.

Step 2: Stars increase by two

Stars form the pyramid shape by increasing count as 2 * i - 1, making odd numbers like 1, 3, 5.

Step 3: Loop controls rows

The outer loop runs from 1 to n, printing one row of the pyramid each time.

🔄

Alternative Approaches

Using string center method
python
n = int(input('Enter number of rows: '))
for i in range(1, n + 1):
    stars = '*' * (2 * i - 1)
    print(stars.center(2 * n - 1))
This method centers the stars automatically, making code cleaner but depends on string centering.
Using while loops
python
n = int(input('Enter number of rows: '))
i = 1
while i <= n:
    print(' ' * (n - i) + '*' * (2 * i - 1))
    i += 1
Uses while loops instead of for loops; functionally same but less common for this pattern.

Complexity: O(n^2) time, O(1) space

Time Complexity

The program runs a loop for each row and prints up to 2*n-1 characters, so time grows roughly with n squared.

Space Complexity

Only a few variables are used; no extra data structures, so space is constant.

Which Approach is Fastest?

Both for-loop and while-loop methods have similar speed; using string center is cleaner but similar in performance.

ApproachTimeSpaceBest For
For loop with spaces and starsO(n^2)O(1)Clear control over spaces and stars
String center methodO(n^2)O(1)Cleaner code, easier alignment
While loop methodO(n^2)O(1)Alternative loop style, less common
💡
Use string multiplication and concatenation to easily build each line of the pyramid.
⚠️
Beginners often forget to decrease spaces or increase stars correctly, breaking the pyramid shape.