Shape Rule
Border only
Print 1 on the first/last row and first/last column — leave interior cells as spaces.

The hollow square of 1s prints a 5×5 border frame — 1 1 1 1 1 on top and bottom, 1 on the sides, spaces inside — a natural step after Program 41’s square pyramid. This tutorial covers nested loops, border conditions, a live preview, worked Python examples, edge cases, and complexity.
Border only
Print 1 on the first/last row and first/last column — leave interior cells as spaces.
Rows (i)
for i in range(1, n + 1): walks each row of the grid.
Columns (j)
for j in range(1, n + 1): visits every column in the current row.
if condition
i == 1 or i == n or j == 1 or j == n — print 1 on the edge, space inside.
3–9 size
Pick a square size and draw the hollow border in the browser.
Complexity
Every cell in an n×n grid is visited once — total checks = n².
A hollow square of 1s prints 1 only on the border of an n×n grid and spaces everywhere else. With n = 5, the output is a 5×5 frame of ones with a hollow center.
In Python nested loops walk every cell (i, j), an if checks whether the cell is on the border, and print(..., end=" ") keeps values on the same line.
It combines nested loops with a boundary condition — a key step after Program 41’s formatted pyramid.
First/last row or column prints 1.
Nested loops visit every (i, j) cell.
Program 41 prints perfect squares; Program 42 prints a hollow frame.
Follow Program 41; continue to Program 43 (right-aligned triangle) next.
In short: for each (i, j) in an n×n grid, print 1 on the border else a space, then print() each row.
Given a grid size n (e.g. 5), print a hollow square border of 1s using nested loops and a border condition.
# n = 5 (conceptual shape)
# 1 1 1 1 1
# 1 1
# 1 1
# 1 1
# 1 1 1 1 1 | Item | Type | Description |
|---|---|---|
n | int | Side length of the square grid — both loops run 1..n. |
i | int | Outer loop — row index from 1 to n. |
j | int | Inner loop — column index from 1 to n. |
for i from 1 to n:
for j from 1 to n:
if i is border or j is border:
print 1
else:
print space
print newline | Approach | Idea | Best for |
|---|---|---|
| Border condition | 1 1 1 1 1 frame | Learning and interviews |
| User-input size | int(input(...)) | Flexible console programs |
| Custom border char | Print * instead of 1 | Visual variety |
| Goal | Pattern |
|---|---|
| Walk rows | for i in range(1, n + 1): |
| Walk columns | for j in range(1, n + 1): |
| Border check | if i == 1 or i == n or j == 1 or j == n: |
| Print border | print("1", end=" ") |
| Print interior | print(" ", end=" ") |
| End the row | print() |
| Program 41 contrast | Perfect-square pyramid — not a hollow grid |
Same hollow square — different ways to control size and border character.
i = 1..nRow index
j = 1..nColumn index
i/j == 1 or nEdge cells print 1
end=" "Keeps grid aligned
Reach for this pattern when teaching boundary conditions with nested loops on a 2D grid.
Natural follow-up — boundary conditions on a grid instead of formatted square pyramids.
Outer/inner bound practice with an immediate visual check.
Combine loops with input() for a flexible square size.
Swap 1 for * on the border — see Example 3.
This is a console teaching pattern — not how you build modern app screens.
Key benefit: one small program that locks in nested loops, output sequencing, and O(n²) thinking.
Choose a square size between 3 and 9 and draw the hollow border in the browser.
Three complete Python programs — fixed size, user input, and asterisk border. Click View Output to reveal sample console results.
Print a 5×5 hollow square border with nested loops and a border condition.
n = 5Hard-coded grid size — ideal for first demos and screenshots.
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() When i = 1 or i = 5, every cell is on the border — all 1s. When i = 3 and j = 3, neither row nor column is on the edge — prints spaces.
Read the square size with input() instead of hard-coding 5.
Read n with input() and validate n ≥ 2 (wrap in try/except ValueError in real apps).
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() Same border-check core as Example 1; only the source of n changes from a literal to user input. Non-numeric input raises ValueError with bare int(input()) — use try/except for safer labs.
Swap 1 for * on the border — same condition, different character.
Keep n = 5 but print * on the border instead of 1.
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("*", end=" ")
else:
print(" ", end=" ")
print() Only the printed character changes — "*" instead of "1" in the if branch. Loop bounds and border check stay the same as Example 1.
No imports needed. Set n = 5 and loop variables i, j.
for i in range(1, n + 1): — walks each row of the grid.
for j in range(1, n + 1): — visits every column in the current row.
if i == 1 or i == n or j == 1 or j == n: — print 1 on the edge, space inside.
print() ends the row after the inner loop finishes.
Every cell in an n×n grid is visited — O(n²) time, O(1) extra memory.
n = 5, row i = 3Trace row 3 cell by cell — which cells print 1 and which print spaces.
j | On border? | Prints |
|---|---|---|
1 | Yes (j == 1) | 1 |
2 | No | |
3 | No | |
4 | No | |
5 | Yes (j == n) | 1 |
Border cells per row = 4n - 4 for n ≥ 2 — total grid visits = n².
Where this tiny pattern (and its loop structure) shows up beyond the homework prompt.
Clearest visual proof that outer and inner bounds interact.
Example: swap 1 for * on the border — see Example 3.
Foundation for inverted, pyramid, diamond, and hollow variants.
Example: continue to Program 43 for a right-aligned number triangle.
Practice print vs row newline without complex math.
Example: put print() inside the inner loop by mistake.
Use separate rows and cols with the same border check.
Example: change both loop bounds and border conditions.
Triangular totals make O(n²) concrete for beginners.
Example: count border cells for n = 5 — total is 16 (4n - 4).
Pair the pattern with input() return checks and positive-row checks.
Example: reject n < 2 and re-prompt.
Pro Tip: when an interviewer asks for patterns, explain the outer/inner roles first — then write the loops. The story matters as much as the code.
Why this pattern earns a permanent spot in beginner Python courses.
Wrong bounds show up immediately as a broken staircase.
Only loops and console output — no arrays or math libraries.
Invert, center, hollow, or change the fill character with small edits.
Streaming output needs no storage beyond loop counters.
Pro Tip: trace (i, j) on paper for n = 3 before coding the full n = 5 demo.
Small habits that keep number-pattern code clean.
end=" " ConsistentBorder cells use print("1", end=" "); interior uses print(" ", end=" ") — same width keeps columns aligned.
try/except ValueErrorUse try/except ValueError so bad input does not crash when converting n.
Only call print() after the inner loop finishes the row.
i == 1 or i == n or j == 1 or j == n covers all four edges in one test.
Trace i = 1, 2, 3 and mark border cells before coding the full n = 5 demo.
Pro Tip: if the output is a vertical list of single digits per line, you almost certainly put print() inside the inner loop.
Mistakes that commonly break hollow square border patterns.
Each cell lands on its own line — you get a column, not a square.
→ Use print("1", end=" ") or print(" ", end=" ") per cell; print() only after the inner loop.
Using i == 5 in the check breaks when n changes to 7 or 10.
→ Always use the n variable: i == n or j == n.
Border prints "1 " but interior prints a single space — columns drift apart.
→ Use two spaces for interior: print(" ", end=" ") to match "1 " width.
n = 1 prints a single 1 with no hollow interior; n = 2 is the thinnest frame.
→ Validate n ≥ 2 for interactive programs expecting a hollow square.
int(input())Letters or empty input raise ValueError with bare int(input()).
→ Catch ValueError and re-prompt on failure.
Check these inputs before calling the solution done.
Output is just 1 on one line — no hollow interior.
Outer loop never runs — print nothing or show a message.
n < 0Treat as invalid; re-prompt instead of silent empty output.
Thinnest hollow frame — four border cells forming a square ring.
Bare int(input()) raises ValueError on bad input — use try/except first.
Each cell visited once — total work grows as n² for an n × n grid.
Try these variations to lock in the pattern.
m*m pyramid1 in every cell — no border checkelse branch"*" instead of "1"1 when i == 1 or i == n or j == 1 or j == n; else print two spaces.print(..., end=" ") stays on the line; print() advances — mix them carefully.n ≥ 2 for interactive programs; n = 1 should print a single 1.n × n grid has n² cells — border cells = 4*n - 4 for n ≥ 2.Quick Takeaway: nested loops over i, j, border check prints "1 ", else " ", then print().
| Program | Time | Extra space |
|---|---|---|
| Nested loops (Examples 1–3) | O(n²) | O(1) |
| Smaller demo (Example 3) | O(n²) | O(1) |
The hollow square border is a compact nested-loop lesson: visit every cell in an n × n grid and use a border condition to print 1 or spaces. Master the fixed-n version, then try user input and a custom border character.
Practice the three examples above, then continue to Program 43 for the next pattern in the series.
Border = first/last row or column — keep cell width consistent ("1 " vs " ") and validate n when reading input.
for i in range(1, n + 1): and for j in range(1, n + 1):if i == 1 or i == n or j == 1 or j == n:"1" on border, " " inside — both with end=" "n ≥ 2 for interactive programsint(input()) in try/except ValueErrorprint() inside the inner cell loop5 in the border conditionn = 1 edge casePrint the pattern the beginner-friendly way.
Border cells only
DefinitionRows i = 1..n
CodeColumns j = 1..n
Codei/j on edge
LogicO(n²) time
AnalysisPrint 1 when i == 1, i == n, j == 1, or j == n; otherwise print spaces. A n × n grid visits n² cells — total prints = n².
Move on to the right-aligned number triangle in the Python number-pattern series.
12 people found this page helpful