Hollow Square of 1s in Python

Beginner
⏱️ 5 min read
📚 Updated: Aug 2025
🎯 2 Code Examples
If Condition

What You’ll Learn

How to print a hollow square pattern of 1s in Python. The key idea is to print 1 on the border (first/last row or first/last column) and print spaces in the inner cells.

This is a classic practice problem for combining nested loops with a simple boundary condition.

⭐ Pattern Output

For n = 5, the pattern looks like this:

Output
1 1 1 1 1
1       1
1       1
1       1
1 1 1 1 1
1

Complete Python Program

We loop through a 5×5 grid. If the current cell is on the boundary, we print 1; otherwise we print a space.

Python
n = 5

for i in range(1, n + 1):
    for j in range(1, n + 1):
        if i == 1 or i == n or j == 1 or j == n:
            print("1", end=" ")
        else:
            print(" ", end=" ")
    print()

🧠 How It Works

1

Set the grid size

n = 5 makes a 5×5 square.

Setup
2

Two loops for rows and columns

The outer loop chooses the row (i) and the inner loop chooses the column (j).

Grid traversal
3

Boundary check

i == 1, i == n, j == 1, or j == n means the cell is on the border, so we print 1.

Condition
4

Print spaces inside

If the cell is not on the border, we print a space so the square becomes hollow.

Hollow center
=

Hollow square frame

We visit every cell in an n×n grid, so runtime is O(n²).

2

Variation — User Input Version

This version lets the user choose the size n at runtime. It also validates that n is at least 2 (a 1×1 frame isn’t very interesting).

Python
n = int(input("Enter the size (n >= 2): "))
if n < 2:
    raise ValueError("n must be at least 2")

for i in range(1, n + 1):
    for j in range(1, n + 1):
        if i == 1 or i == n or j == 1 or j == n:
            print("1", end=" ")
        else:
            print(" ", end=" ")
    print()

💡 Tips for Enhancement

Try These

  • Print a different border character (like 0, #, or *)
  • Use range(n) (0-based) and check i == 0 / i == n-1
  • Print a hollow rectangle by using different rows and cols
  • Fill the inside with a different digit to create a framed pattern
  • Add spacing control (e.g., two spaces after each 1) for wider output

Avoid

  • Forgetting the end=" " which keeps values on the same line
  • Mixing row and column checks (keep the boundary condition clear)
  • Using n < 2 without handling it (output will be confusing)
  • Printing tabs inconsistently (spaces are more predictable across consoles)

Key Takeaways

1

Use nested loops to iterate through an n×n grid.

2

Print 1 when you are on the border: first/last row or first/last column.

3

Print spaces for inner cells to make the square hollow.

4

Total checks are , so runtime is O(n²).

❓ Frequently Asked Questions

Because we print spaces for cells that are not on the border, leaving the middle hollow.
Cells where i is 1 or n (top/bottom row) or j is 1 or n (left/right column).
Yes. Use separate variables like rows and cols, and check boundaries with those limits.
O(n²), since we evaluate every cell in the grid.

Explore More Python Number Patterns!

Keep practicing loops by trying diamonds, pyramids, and framed patterns.

All Number Patterns →
Did you know?

A hollow n×n frame contains 4n-4 border cells (for n ≥ 2). Everything else is inside the frame.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful