Mirror Number & Asterisk Pattern in Python

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

What You’ll Learn

How to print a mirror number pattern with an expanding center of asterisks in Python, like:

  • 1234554321
  • 1234**4321
  • 123****321
  • 12******21
  • 1********1

This is a great nested-loop exercise: one loop prints the left numbers, one prints the center, and one prints the right numbers.

⭐ Pattern Output

For 5 rows, the pattern looks like this:

Output
1234554321
1234**4321
123****321
12******21
1********1
1

Complete Python Program

The outer loop decreases the numeric range, while the middle loop prints ** blocks that grow each row to keep symmetry.

Python
for i in range(6, 1, -1):
    for j in range(1, i):
        print(j, end="")
    for k in range(i, 6):
        print("**", end="")
    for m in range(i - 1, 0, -1):
        print(m, end="")
    print()

🧠 How It Works

1

Outer loop shrinks the number width

for i in range(6, 1, -1) sets row sizes 6, 5, 4, 3, 2. Each next row prints fewer numbers on both sides.

Row control
2

Left side prints ascending numbers

for j in range(1, i) prints 1 to i - 1 without spaces.

Left half
3

Middle grows with double asterisks

for k in range(i, 6) prints ** repeated 6 - i times, which expands the center each row.

Center
4

Right side prints descending numbers

for m in range(i - 1, 0, -1) prints i - 1 down to 1, completing the mirror.

Right half
=

Symmetric mirror pattern

Each row prints numbers and asterisks in O(n) time, repeated for \(n\) rows, so total time is O(n²).

2

Variation — User Input Version

Let the user choose how many rows to print. This version keeps the same idea: shrinking numbers and expanding center.

Python
rows = int(input("Enter the number of rows: "))

for i in range(rows + 1, 1, -1):
    for j in range(1, i):
        print(j, end="")
    for k in range(i, rows + 1):
        print("**", end="")
    for m in range(i - 1, 0, -1):
        print(m, end="")
    print()

💡 Tips for Enhancement

Try These

  • Validate input (reject rows < 1) before printing
  • Use "*" instead of "**" and adjust the loop counts to keep symmetry
  • Add spaces between digits for readability (then update expected output)
  • Replace numbers with letters to create alphabet mirror patterns
  • Build each row as a string for faster printing in large patterns

Avoid

  • Forgetting the final print() (rows will run together)
  • Mixing end="" and default prints (it breaks alignment)
  • Assuming input is always numeric (wrap int() conversion if needed)
  • Changing the center width without updating the right side loop

Key Takeaways

1

The pattern is built from three parts: left numbers, center asterisks, right numbers.

2

As the left/right numeric range shrinks, the center expands to keep the pattern symmetric.

3

Using print(..., end="") is key for printing multiple segments on one line.

4

This is a reusable template for many mirror patterns (numbers, stars, alphabets).

❓ Frequently Asked Questions

In the first row, i = 6, so range(i, 6) is empty and the center prints nothing. As i decreases, the center loop runs more times.
Yes. Change print("**", end="") to print("*", end="") and adjust the loop bounds so the total width stays symmetric.
Use the user-input version and increase rows. The outer loop will automatically print more rows and expand the center accordingly.
O(n²) for n rows, since each row prints \(O(n)\) characters and there are \(n\) rows.

Explore More Python Number Patterns!

Keep practicing symmetry and loop control with more number pattern programs.

All Number Patterns →
Did you know?

Mirror patterns like this are closely related to building palindromic strings. The left side and right side are generated separately, then “glued” together with a center block.

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