Hollow X Number Pattern in Python

What You’ll Learn
How to print a hollow X pattern using numbers in Python. The row number appears on the two diagonals, and all other positions are spaces.
This pattern is a clean exercise for practicing nested loops and diagonal conditions.
⭐ Pattern Output
For n = 5, the pattern looks like this:
1 1
2 2
3 3
4 4
5Complete Python Program
We print the row number on both diagonals and print spaces everywhere else.
n = 5
for i in range(1, n + 1):
for j in range(1, n + 1):
if i == j:
print(i, end="")
else:
print(" ", end="")
for k in range(n - 1, 0, -1):
if i == k:
print(i, end="")
else:
print(" ", end="")
print()🧠 How It Works
Pick the grid size
n = 5 means we print an X inside a 5×5 grid (with spaces included).
Outer loop controls the row number
for i in range(1, n + 1) iterates rows 1 to n, and the value i is what gets printed on the diagonals.
Left diagonal check
In the first inner loop, we print i when j == i. Otherwise we print a space.
Right diagonal check
In the second inner loop, we print i when the column matches the other diagonal (equivalent to j == n - i + 1). Otherwise we print spaces.
X shape
We scan every cell of an \(n \times n\) grid, so runtime is O(n²).
Variation — User Input Version
Let the user choose the size. This version keeps the same two-part printing style (left part + right part) as the main example.
n = int(input("Enter n: "))
if n < 1:
raise ValueError("n must be at least 1")
for i in range(1, n + 1):
for j in range(1, n + 1):
if i == j:
print(i, end="")
else:
print(" ", end="")
for k in range(n - 1, 0, -1):
if i == k:
print(i, end="")
else:
print(" ", end="")
print()💡 Tips for Enhancement
Try These
- Print
*instead of numbers for a classic X star pattern - Use a fixed-width cell (like two spaces) for better alignment with multi-digit rows
- Try odd sizes to see a single center intersection
- Print the border also to combine X + box patterns
- Build each row as a list and
joinit for easier formatting
Avoid
- Printing without spaces when n grows (the X becomes hard to see)
- Mixing 0-based and 1-based indices in diagonal formulas
- Forgetting the newline at the end of each row
- Using large n with multi-digit i values without padding
Key Takeaways
Print on the main diagonal when j == i.
Print on the anti-diagonal when j == n - i + 1.
All other cells are spaces, creating the hollow look.
We visit every cell in an \(n \times n\) grid, so it’s O(n²).
❓ Frequently Asked Questions
i when j == i or j == n - i + 1. Otherwise print a space.n is even, the diagonals cross between cells, so there is no single center cell to print.print(f"{i:2}", end="") and spaces of the same width) so columns stay aligned.Explore More Python Number Patterns!
Diagonal patterns are a great stepping stone to matrix traversals and 2D indexing problems.
The two diagonal conditions j == i and j == n - i + 1 are the same checks used in many matrix problems (like printing diagonals or detecting an X pattern in grids).
7 people found this page helpful
