0
0
PythonProgramBeginner · 2 min read

Python Program to Print Pyramid Pattern

You can print a pyramid 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 levels.
📋

Examples

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

How to Think About It

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

Algorithm

1
Get the number of levels (n) for the pyramid.
2
For each line from 1 to n:
3
Calculate and print spaces equal to (n - current line number).
4
Calculate and print stars equal to (2 * current line number - 1).
5
Move to the next line.
💻

Code

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

Dry Run

Let's trace the pyramid pattern for n=3 through the code

1

Input

n = 3

2

First line (i=1)

spaces = 3 - 1 = 2, stars = 2*1 - 1 = 1, print ' *'

3

Second line (i=2)

spaces = 3 - 2 = 1, stars = 2*2 - 1 = 3, print ' ***'

4

Third line (i=3)

spaces = 3 - 3 = 0, stars = 2*3 - 1 = 5, print '*****'

ispacesstarsline output
121 *
213 ***
305*****
💡

Why This Works

Step 1: Spaces decrease each line

We print n - i spaces to push stars to the right, creating the pyramid shape.

Step 2: Stars increase by two

We print 2 * i - 1 stars to form the pyramid's width, growing by two each line.

Step 3: Loop controls lines

The outer loop runs from 1 to n, controlling how many lines the pyramid has.

🔄

Alternative Approaches

Using while loops
python
n = int(input('Enter levels: '))
i = 1
while i <= n:
    print(' ' * (n - i) + '*' * (2 * i - 1))
    i += 1
This uses while loops instead of for loops; functionally the same but less concise.
Using string center method
python
n = int(input('Enter levels: '))
for i in range(1, n + 1):
    print(('*' * (2 * i - 1)).center(2 * n - 1))
This centers the stars automatically, making code cleaner and easier to read.

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

Time Complexity

The program uses nested printing for each line, with the number of stars and spaces growing linearly, resulting in O(n^2) time.

Space Complexity

Only a few variables are used, and printing is done directly, so space complexity is O(1).

Which Approach is Fastest?

All approaches have similar time complexity; using string center method improves readability but not speed.

ApproachTimeSpaceBest For
For loops with manual spacesO(n^2)O(1)Clear control over spaces and stars
While loopsO(n^2)O(1)Beginners learning loop basics
String center methodO(n^2)O(1)Cleaner code and readability
💡
Use string multiplication and concatenation to easily build each line of the pyramid.
⚠️
Beginners often forget to adjust spaces, causing the pyramid to be left-aligned instead of centered.