Hollow X Number Pattern in Python

Beginner
⏱️ 6 min read
📚 Updated: Aug 2025
🎯 2 Code Examples
Diagonal Conditions

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:

Output
1       1
 2     2
  3   3
   4 4
    5
1

Complete Python Program

We print the row number on both diagonals and print spaces everywhere else.

Python
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

1

Pick the grid size

n = 5 means we print an X inside a 5×5 grid (with spaces included).

Setup
2

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.

Row control
3

Left diagonal check

In the first inner loop, we print i when j == i. Otherwise we print a space.

Main diagonal
4

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.

Anti-diagonal
=

X shape

We scan every cell of an \(n \times n\) grid, so runtime is O(n²).

2

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.

Python
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 join it 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

1

Print on the main diagonal when j == i.

2

Print on the anti-diagonal when j == n - i + 1.

3

All other cells are spaces, creating the hollow look.

4

We visit every cell in an \(n \times n\) grid, so it’s O(n²).

❓ Frequently Asked Questions

Use one loop for columns and print i when j == i or j == n - i + 1. Otherwise print a space.
When n is even, the diagonals cross between cells, so there is no single center cell to print.
Print each cell with a fixed width (for example using print(f"{i:2}", end="") and spaces of the same width) so columns stay aligned.
O(n²), because we iterate over an \(n \times n\) grid.

Explore More Python Number Patterns!

Diagonal patterns are a great stepping stone to matrix traversals and 2D indexing problems.

All Number Patterns →
Did you know?

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).

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.

7 people found this page helpful