Split Mirror Number Pattern in Python

What You’ll Learn
How to print a split mirror number pattern in Python, where the left side grows and the right side mirrors it:
1 112 21123 3211234 43211234554321
This pattern uses two loops per row and prints spaces when the current position is beyond the row width.
⭐ Pattern Output
For 5 rows, the pattern looks like this:
1 1
12 21
123 321
1234 4321
1234554321Complete Python Program
Each row prints the left half (1..i) with spaces, then prints the right half (i..1) with spaces, creating a split mirror effect.
for i in range(1, 6):
for j in range(1, 6):
if j <= i:
print(j, end="")
else:
print(" ", end="")
for k in range(5, 0, -1):
if k <= i:
print(k, end="")
else:
print(" ", end="")
print()🧠 How It Works
Outer loop controls rows
for i in range(1, 6) prints 5 rows and sets how many digits are visible on each side.
Left loop prints 1..i
for j in range(1, 6) prints j only when j <= i. Otherwise it prints a space, keeping the left block width constant.
Right loop prints i..1
for k in range(5, 0, -1) prints k only when k <= i. Otherwise it prints spaces to maintain alignment.
Print a newline
print() moves to the next row.
Split mirror effect
Each row prints \(2n\) positions (here \(n=5\)), so overall runtime is O(n²) for \(n\) rows.
Variation — User Input Version
Choose the number of rows. This version automatically scales the left and right halves.
rows = int(input("Enter the number of rows: "))
for i in range(1, rows + 1):
for j in range(1, rows + 1):
if j <= i:
print(j, end="")
else:
print(" ", end="")
for k in range(rows, 0, -1):
if k <= i:
print(k, end="")
else:
print(" ", end="")
print()💡 Tips for Enhancement
Try These
- Validate input (reject
rows < 1) before printing - Replace spaces with dots (
.) to visualize the alignment grid - Center the whole output by adding leading spaces per row
- Add a separator between halves (like
|) - Build each row as a string for better performance on large sizes
Avoid
- Mixing
end=""and default prints in the same row - Changing loop bounds without updating expected output
- Forgetting the newline print at the end of each row
- Assuming user input is always valid (wrap
int()conversion if needed)
Key Takeaways
Two loops build the left and right halves of the mirror.
Spaces keep each half a constant width until the last row.
The final row has no spaces in the middle, forming 1234554321.
Overall runtime is O(n²) due to nested loops.
❓ Frequently Asked Questions
i reaches the maximum (5), both loops print all digits (no spaces), so the left half is 12345 and the right half is 54321, forming 1234554321.i. Changing the total width (rows) changes how many spaces appear.Explore More Python Number Patterns!
Try more mirror and gap-based patterns to improve your loop control.
Split patterns are a practical way to learn “print value else print space” logic, which is the foundation of many alignment and formatting tasks.
12 people found this page helpful
