X Pattern with 0 and * in Python

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

What You’ll Learn

How to print a compact X pattern using * and 0. The idea is to print * on the diagonals and on the middle column, and print 0 everywhere else.

This is a useful exercise for understanding index relationships like i == j and i + j == constant.

⭐ Pattern Output

For a 4×9 grid, the pattern looks like this:

Output
*000*000*
0*00*00*0
00*0*0*00
000***000
1

Complete Python Program

We iterate over rows and columns. If the current position is on a diagonal or the middle column, print *. Otherwise print 0.

Python
rows = 4
cols = 9
mid = 5  # middle column (1-based)

for i in range(1, rows + 1):
    for j in range(1, cols + 1):
        if i == j or j == mid or i == (cols + 1) - j:
            print("*", end="")
        else:
            print("0", end="")
    print()

🧠 How It Works

1

Define the grid

rows = 4 and cols = 9 set the output size.

Setup
2

Walk through every cell

The nested loops iterate over each (i, j) position in the grid.

Traversal
3

Check the diagonals and middle

We print * when any of these are true: i == j (left diagonal), i == (cols + 1) - j (right diagonal), or j == mid (middle column).

Condition
4

Fill the rest with 0

If none of the conditions match, the cell is not part of the pattern lines, so we print 0.

Fill
=

X pattern with 0 and *

We check each cell once, so time complexity is O(rows×cols).

2

Variation — User Input Version

To make the pattern scalable, we take an odd cols value so there is a clear middle column. This variation prints a square grid for simplicity.

Python
n = int(input("Enter an odd size (e.g., 9, 11, 13): "))
if n % 2 == 0:
    raise ValueError("Please enter an odd number")

mid = (n + 1) // 2

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

💡 Tips for Enhancement

Try These

  • Replace 0 with a space to make the X stand out more
  • Print only diagonals (remove the middle-column condition) for a thin X
  • Use different symbols for diagonals and middle line
  • Convert to 0-based indexing and use i == j and i + j == n - 1
  • Make a hollow X by printing stars only at intersection points

Avoid

  • Using an even width when you need a single middle column
  • Forgetting to print a newline after each row
  • Hardcoding dimensions when you want a scalable pattern
  • Mixing 0-based and 1-based indices in the same formula

Key Takeaways

1

Use i == j and i == n+1-j to detect diagonals.

2

The middle column is (n+1)//2 for odd n.

3

Print * on pattern positions and 0 elsewhere.

4

Runtime is O(n²) for an n×n grid.

❓ Frequently Asked Questions

In a grid, i == j identifies the main diagonal. Similarly, i == n+1-j identifies the other diagonal (1-based indexing).
Only if you want a single middle column line. With an even size, there isn’t one exact center column.
Print " " instead of "0" in the else branch. Keep in mind the output will look wider depending on your console font.
O(n²) for an n×n grid because we check each cell once.

Explore More Python Number Patterns!

Try diagonal patterns, hollow frames, and pyramids to get fluent with loop logic.

All Number Patterns →
Did you know?

In 0-based indexing, the two diagonals of an n×n grid are given by i == j and i + j == n - 1.

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