X Pattern with 0 and * in Python

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:
*000*000*
0*00*00*0
00*0*0*00
000***000Complete Python Program
We iterate over rows and columns. If the current position is on a diagonal or the middle column, print *. Otherwise print 0.
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
Define the grid
rows = 4 and cols = 9 set the output size.
Walk through every cell
The nested loops iterate over each (i, j) position in the grid.
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).
Fill the rest with 0
If none of the conditions match, the cell is not part of the pattern lines, so we print 0.
X pattern with 0 and *
We check each cell once, so time complexity is O(rows×cols).
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.
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
0with 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 == jandi + 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
Use i == j and i == n+1-j to detect diagonals.
The middle column is (n+1)//2 for odd n.
Print * on pattern positions and 0 elsewhere.
Runtime is O(n²) for an n×n grid.
❓ Frequently Asked Questions
i == j identifies the main diagonal. Similarly, i == n+1-j identifies the other diagonal (1-based indexing)." " instead of "0" in the else branch. Keep in mind the output will look wider depending on your console font.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.
In 0-based indexing, the two diagonals of an n×n grid are given by i == j and i + j == n - 1.
8 people found this page helpful
