Mirror Number & Asterisk Pattern in Python

What You’ll Learn
How to print a mirror number pattern with an expanding center of asterisks in Python, like:
12345543211234**4321123****32112******211********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:
1234554321
1234**4321
123****321
12******21
1********1Complete Python Program
The outer loop decreases the numeric range, while the middle loop prints ** blocks that grow each row to keep symmetry.
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
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.
Left side prints ascending numbers
for j in range(1, i) prints 1 to i - 1 without spaces.
Middle grows with double asterisks
for k in range(i, 6) prints ** repeated 6 - i times, which expands the center each row.
Right side prints descending numbers
for m in range(i - 1, 0, -1) prints i - 1 down to 1, completing the mirror.
Symmetric mirror pattern
Each row prints numbers and asterisks in O(n) time, repeated for \(n\) rows, so total time is O(n²).
Variation — User Input Version
Let the user choose how many rows to print. This version keeps the same idea: shrinking numbers and expanding center.
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
The pattern is built from three parts: left numbers, center asterisks, right numbers.
As the left/right numeric range shrinks, the center expands to keep the pattern symmetric.
Using print(..., end="") is key for printing multiple segments on one line.
This is a reusable template for many mirror patterns (numbers, stars, alphabets).
❓ Frequently Asked Questions
i = 6, so range(i, 6) is empty and the center prints nothing. As i decreases, the center loop runs more times.print("**", end="") to print("*", end="") and adjust the loop bounds so the total width stays symmetric.rows. The outer loop will automatically print more rows and expand the center accordingly.Explore More Python Number Patterns!
Keep practicing symmetry and loop control with more number pattern programs.
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.
12 people found this page helpful
