Border Number Square Pattern in Python

What You’ll Learn
How to print a border number square in Python. Only the outer boundary contains numbers; the inside stays blank.
This is a great exercise for practicing 2D grids, indexing, and formatting output neatly.
⭐ Pattern Output
For n = 5, the pattern looks like this:
1 2 3 4 5
16 6
15 7
14 8
13 12 11 10 9Complete Python Program
Fill the border in four passes (top, right, bottom, left), then print the grid with fixed-width formatting.
n = 5
grid = [["" for _ in range(n)] for _ in range(n)]
val = 1
# top row
for j in range(n):
grid[0][j] = val
val += 1
# right column
for i in range(1, n):
grid[i][n - 1] = val
val += 1
# bottom row (right to left)
for j in range(n - 2, -1, -1):
grid[n - 1][j] = val
val += 1
# left column (bottom to top)
for i in range(n - 2, 0, -1):
grid[i][0] = val
val += 1
for i in range(n):
for j in range(n):
if grid[i][j] == "":
print(f"{'':>3}", end="")
else:
print(f"{grid[i][j]:>3}", end="")
print()🧠 How It Works
Create an empty grid
We build an n × n grid and leave the inside blank.
Fill the top and right borders
First we fill the top row left-to-right, then the right column top-to-bottom using a running counter val.
Fill the bottom and left borders
Next we fill the bottom row right-to-left, and finally fill the left column bottom-to-top.
Print with fixed-width formatting
We print each cell with width 3 so columns stay aligned even with multi-digit numbers.
Border-only fill
Only the boundary gets numbers; the interior stays blank.
Variation — User Input Version
Read n from the user and generate the border-filled square.
n = int(input("Enter n: "))
if n < 1:
raise ValueError("n must be at least 1")
grid = [["" for _ in range(n)] for _ in range(n)]
val = 1
for j in range(n):
grid[0][j] = val
val += 1
for i in range(1, n):
grid[i][n - 1] = val
val += 1
for j in range(n - 2, -1, -1):
grid[n - 1][j] = val
val += 1
for i in range(n - 2, 0, -1):
grid[i][0] = val
val += 1
for row in grid:
for cell in row:
print(f"{cell:>3}" if cell != "" else f"{'':>3}", end="")
print()💡 Tips for Enhancement
Try These
- Fill multiple layers (spiral) instead of only the outer border
- Use a different start value instead of 1
- Replace numbers with letters to create an alphabet border
- Store the grid and reuse it for other pattern operations
- Print separators (like
|) between columns for debugging
Avoid
- Printing without fixed width (multi-digit numbers break alignment)
- Forgetting to skip the corners when filling multiple sides (duplicates)
- Using
n < 1without validation - Mixing strings and ints without consistent formatting
Key Takeaways
Fill the border in four passes: top, right, bottom, left.
Keep a running counter to place consecutive numbers.
Use fixed-width printing to keep columns aligned.
Printing an \(n \times n\) grid costs O(n²).
❓ Frequently Asked Questions
Explore More Python Number Patterns!
Border patterns are a stepping stone to spiral and matrix traversal problems.
The same border-filling idea is used to solve spiral matrix problems—just repeat the border fill for inner layers.
7 people found this page helpful
