0-Centered Mirror Number Pattern in Python

What You’ll Learn
How to print a 0-centered mirror number pattern in Python, where each row increases up to 9, prints 0, then decreases back:
090989098...1234567890987654321
This is a fun symmetry pattern that uses two loops (up and down) with a fixed center digit.
⭐ Pattern Output
The pattern for 10 rows looks like this:
0
909
89098
7890987
678909876
56789098765
4567890987654
345678909876543
23456789098765432
1234567890987654321Complete Python Program
The first loop prints the increasing part up to 9, then prints a center 0, and finally prints the decreasing part back to the start digit.
for i in range(10, 0, -1):
for j in range(i, 10):
print(j, end="")
print("0", end="")
for k in range(9, i - 1, -1):
print(k, end="")
print()🧠 How It Works
Outer loop controls the starting digit
for i in range(10, 0, -1) makes i go from 10 down to 1, which effectively controls how many digits appear on each side.
Print the increasing left part
for j in range(i, 10) prints from i to 9. When i is large (like 10), this prints nothing—so the first row is just 0.
Print the center 0
print("0", end="") ensures every row is centered on the digit 0.
Print the decreasing right part
for k in range(9, i - 1, -1) prints from 9 down to i, forming the mirror on the right.
0-centered mirror pattern
The row length increases by 2 each line (except the center), and the full output grows quadratically in total characters, so the overall runtime is O(n²).
Variation — User Input Version
Choose the maximum digit (like 9, 7, or 5) to control the pattern size.
max_digit = int(input("Enter max digit (e.g., 9): "))
for i in range(max_digit + 1, 0, -1):
for j in range(i, max_digit + 1):
print(j % 10, end="")
print("0", end="")
for k in range(max_digit, i - 1, -1):
print(k % 10, end="")
print()💡 Tips for Enhancement
Try These
- Validate input (reject
max_digit < 0) before printing - Add spaces between digits for readability (then update output)
- Replace the center
0with another character (like*) - Store each row in a variable and print once for large patterns
- Generate the same pattern with strings instead of print loops
Avoid
- Forgetting the final
print()after each row - Mixing default print behavior with
end=""mid-row - Using very large max digits without considering output size
- Changing loop ranges without updating the expected output section
Key Takeaways
Each row has an increasing part, a center 0, and a decreasing part.
range(i, 10) and range(9, i - 1, -1) generate the left/right halves.
The first row prints just 0 because the left half is empty when i = 10.
Total output size grows quickly, so overall runtime is O(n²) for \(n\) rows.
❓ Frequently Asked Questions
i = 10 so range(i, 10) is empty and prints nothing. Then the program prints the center 0 and the right loop prints nothing extra beyond the logic, producing 0.i = 9, the left loop prints 9, then the center prints 0, then the right loop prints 9 again, creating 909.print("0", end="") with print("*", end="") or any other character.Explore More Python Number Patterns!
Try more mirror and symmetry-based patterns to master nested loops.
Patterns like this resemble building a string from two halves. Many problems in programming (like palindromes) use the same “construct then mirror” technique.
12 people found this page helpful
