0
0
PythonProgramBeginner · 2 min read

Python Program to Print Hollow Square Star Pattern

You can print a hollow square star pattern in Python using nested loops where you print stars on the border and spaces inside, like for i in range(n): for j in range(n): print('*' if i==0 or i==n-1 or j==0 or j==n-1 else ' ', end='').
📋

Examples

Inputn = 3
Output*** * * ***
Inputn = 5
Output***** * * * * * * *****
Inputn = 1
Output*
🧠

How to Think About It

To print a hollow square star pattern, think of a square grid of size n by n. You print stars on the first and last rows and columns to form the border. For the inside rows and columns, print spaces instead of stars to create the hollow effect.
📐

Algorithm

1
Get the size n of the square from the user or input.
2
Loop through each row from 0 to n-1.
3
Inside that, loop through each column from 0 to n-1.
4
If the current position is on the first or last row, or first or last column, print a star.
5
Otherwise, print a space.
6
After each row, move to the next line.
💻

Code

python
n = 5
for i in range(n):
    for j in range(n):
        if i == 0 or i == n-1 or j == 0 or j == n-1:
            print('*', end='')
        else:
            print(' ', end='')
    print()
Output
***** * * * * * * *****
🔍

Dry Run

Let's trace the code for n = 3 through the loops and print decisions.

1

First row (i=0)

All columns j=0 to 2 print '*', so output: ***

2

Second row (i=1)

Columns j=0 and j=2 print '*', j=1 prints space, output: * *

3

Third row (i=2)

All columns j=0 to 2 print '*', output: ***

Row (i)Column (j)Print
00*
01*
02*
10*
11
12*
20*
21*
22*
💡

Why This Works

Step 1: Print border stars

The code prints stars when the current row or column is the first or last, creating the square's border with *.

Step 2: Print spaces inside

For positions inside the border, the code prints spaces to make the square hollow using print(' ').

Step 3: Use nested loops

Nested loops let us check every position in the square grid row by row and column by column.

🔄

Alternative Approaches

Using string multiplication and conditional expression
python
n = 5
for i in range(n):
    print(''.join('*' if i==0 or i==n-1 or j==0 or j==n-1 else ' ' for j in range(n)))
This method uses a single print per row with string join, making code shorter and more Pythonic but slightly harder for beginners.
Using functions to separate logic
python
def hollow_square(n):
    for i in range(n):
        for j in range(n):
            if i in (0, n-1) or j in (0, n-1):
                print('*', end='')
            else:
                print(' ', end='')
        print()

hollow_square(5)
This approach wraps the pattern in a function for reuse and clarity.

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

Time Complexity

The program uses two nested loops each running n times, so it runs in O(n^2) time.

Space Complexity

It uses constant extra space O(1) because it prints directly without storing the pattern.

Which Approach is Fastest?

Both main and alternative methods run in O(n^2) time; the difference is mainly readability and style.

ApproachTimeSpaceBest For
Nested loops with if-elseO(n^2)O(1)Beginners, clarity
String join with conditionalO(n^2)O(1)Concise code, Pythonic style
Function encapsulationO(n^2)O(1)Reusable code, modular design
💡
Remember to print a newline after each row to move to the next line.
⚠️
Beginners often forget to print spaces inside the square, resulting in a solid square instead of hollow.