0-Centered Mirror Number Pattern in Python

Beginner
⏱️ 7 min read
📚 Updated: Aug 2025
🎯 2 Code Examples
Nested Loops

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:

  • 0
  • 909
  • 89098
  • ...
  • 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:

Output
0
909
89098
7890987
678909876
56789098765
4567890987654
345678909876543
23456789098765432
1234567890987654321
1

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

Python
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

1

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.

Row control
2

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.

Left half
3

Print the center 0

print("0", end="") ensures every row is centered on the digit 0.

Center
4

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.

Right half
=

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

2

Variation — User Input Version

Choose the maximum digit (like 9, 7, or 5) to control the pattern size.

Python
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 0 with 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

1

Each row has an increasing part, a center 0, and a decreasing part.

2

range(i, 10) and range(9, i - 1, -1) generate the left/right halves.

3

The first row prints just 0 because the left half is empty when i = 10.

4

Total output size grows quickly, so overall runtime is O(n²) for \(n\) rows.

❓ Frequently Asked Questions

In the first iteration, 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.
For i = 9, the left loop prints 9, then the center prints 0, then the right loop prints 9 again, creating 909.
Yes. Replace print("0", end="") with print("*", end="") or any other character.
O(n²) for \(n\) rows, since total printed digits grow roughly quadratically with the number of rows.

Explore More Python Number Patterns!

Try more mirror and symmetry-based patterns to master nested loops.

All Number Patterns →
Did you know?

Patterns like this resemble building a string from two halves. Many problems in programming (like palindromes) use the same “construct then mirror” technique.

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.

12 people found this page helpful