0
0
PythonProgramBeginner · 2 min read

Python Program to Print Hollow Square Pattern

You can print a hollow square pattern in Python using nested loops where the first and last rows and columns print stars with 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 = 1
Output*
Inputn = 4
Output**** * * * * ****
Inputn = 6
Output****** * * * * * * * * ******
🧠

How to Think About It

To print a hollow square, think of a grid with rows and columns. Print stars on the edges (first and last rows, first and last columns) and spaces inside. Use two loops: one for rows and one for columns, and decide what to print based on position.
📐

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 each row, 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 n=4 through the code to see how the hollow square is printed.

1

First row (i=0)

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

2

Second row (i=1)

Columns j=0 and j=3 print '*', others print space, output: * *

3

Third row (i=2)

Same as second row, output: * *

4

Last row (i=3)

All columns print '*', output: ****

Row iColumn jPrint
00-3*
10*
11-2
13*
20*
21-2
23*
30-3*
💡

Why This Works

Step 1: Print stars on edges

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

Step 2: Print spaces inside

For positions not on the edges, the code prints spaces to make the square hollow.

Step 3: Use nested loops

Two loops go through each row and column, deciding what to print based on position.

🔄

Alternative Approaches

Using string multiplication for edges
python
n = 5
for i in range(n):
    if i == 0 or i == n-1:
        print('*' * n)
    else:
        print('*' + ' ' * (n-2) + '*')
This method is shorter and uses string multiplication but is less flexible for complex patterns.
Using list comprehension and join
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 approach uses a single line inside the loop for printing, making code concise but slightly harder for beginners.

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

The program uses only a few variables and prints directly, so space complexity is O(1).

Which Approach is Fastest?

All approaches run in O(n^2) time; string multiplication is simpler and slightly faster for edges, but nested loops offer more flexibility.

ApproachTimeSpaceBest For
Nested loops with ifO(n^2)O(1)Flexible patterns
String multiplicationO(n^2)O(1)Simple hollow squares
List comprehensionO(n^2)O(1)Concise code
💡
Remember to print stars on the first and last rows and columns to create the hollow border.
⚠️
Beginners often forget to print spaces inside, resulting in a solid square instead of hollow.