0
0
PythonProgramBeginner · 2 min read

Python Program to Print Hollow Pyramid Pattern

You can print a hollow pyramid pattern in Python using nested loops where you print stars only at the edges and spaces inside, for example: for i in range(1, n+1): print(' '*(n-i) + '*' + ' '*(2*i-3) + ('*' if i>1 else '')).
📋

Examples

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

How to Think About It

To print a hollow pyramid, think of each row as having spaces on the left to center the stars, then print a star at the start and end of the row, and spaces in between for hollow effect. The last row is filled with stars to complete the pyramid base.
📐

Algorithm

1
Get the number of rows (n) from the user.
2
For each row from 1 to n:
3
Print spaces to center the pyramid.
4
If it is the first row, print a single star.
5
If it is the last row, print all stars to form the base.
6
Otherwise, print a star, then spaces, then another star to create the hollow effect.
7
Move to the next line.
💻

Code

python
n = int(input("Enter the number of rows: "))
for i in range(1, n + 1):
    print(' ' * (n - i), end='')
    if i == 1:
        print('*')
    elif i == n:
        print('*' * (2 * n - 1))
    else:
        print('*' + ' ' * (2 * i - 3) + '*')
Output
Enter the number of rows: 5 * * * * * * * *********
🔍

Dry Run

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

1

Row 1

Print 2 spaces, then a single star: ' *'

2

Row 2

Print 1 space, star, 1 space, star: ' * *'

3

Row 3

Print 0 spaces, then 5 stars: '*****'

RowSpaces before starsStars and spaces printed
12*
21* *
30*****
💡

Why This Works

Step 1: Centering the pyramid

We print n - i spaces before stars to center the pyramid on each row.

Step 2: Printing stars on edges

For rows except the first and last, we print a star at the start and end of the row to create the hollow effect.

Step 3: Filling the base

On the last row, we print all stars without spaces to form the solid base of the pyramid.

🔄

Alternative Approaches

Using string join and conditional expressions
python
n = int(input('Enter rows: '))
for i in range(1, n+1):
    line = [' '] * (n - i) + ['*']
    if i == 1:
        print(''.join(line))
    elif i == n:
        print('*' * (2*n - 1))
    else:
        line += [' '] * (2*i - 3) + ['*']
        print(''.join(line))
This method builds each line as a list and joins it, which can be easier to modify but uses more memory.
Using recursion to print each row
python
def print_row(i, n):
    if i > n:
        return
    print(' ' * (n - i), end='')
    if i == 1:
        print('*')
    elif i == n:
        print('*' * (2*n - 1))
    else:
        print('*' + ' ' * (2*i - 3) + '*')
    print_row(i+1, n)

n = int(input('Enter rows: '))
print_row(1, n)
Recursion replaces the loop, which is elegant but less efficient for large n.

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

Time Complexity

The program uses nested loops implicitly by printing spaces and stars for each row, leading to O(n^2) time as the number of characters printed grows with the square of n.

Space Complexity

The program uses constant extra space, only storing counters and temporary strings, so space complexity is O(1).

Which Approach is Fastest?

All approaches have similar time complexity; the iterative loop method is simplest and most efficient in practice.

ApproachTimeSpaceBest For
Iterative loopsO(n^2)O(1)Simplicity and efficiency
String join with listO(n^2)O(n)Easier line construction, more memory
RecursionO(n^2)O(n)Elegant code, less efficient for large n
💡
Remember to print spaces before stars to center the pyramid properly.
⚠️
Beginners often forget to print the last row fully filled with stars, breaking the pyramid shape.