Split Mirror Number Pattern in Python

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

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        1
  • 12      21
  • 123    321
  • 1234  4321
  • 1234554321

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:

Output
1        1
12      21
123    321
1234  4321
1234554321
1

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

Python
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

1

Outer loop controls rows

for i in range(1, 6) prints 5 rows and sets how many digits are visible on each side.

Row control
2

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.

Left half
3

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.

Right half
4

Print a newline

print() moves to the next row.

Line break
=

Split mirror effect

Each row prints \(2n\) positions (here \(n=5\)), so overall runtime is O(n²) for \(n\) rows.

2

Variation — User Input Version

Choose the number of rows. This version automatically scales the left and right halves.

Python
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

1

Two loops build the left and right halves of the mirror.

2

Spaces keep each half a constant width until the last row.

3

The final row has no spaces in the middle, forming 1234554321.

4

Overall runtime is O(n²) due to nested loops.

❓ Frequently Asked Questions

When 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.
The gap is created by printing spaces in positions where the index is greater than i. Changing the total width (rows) changes how many spaces appear.
Yes. Replace the printed digits with stars or letters, and keep the same spacing logic to preserve the split mirror structure.
O(n²) for \(n\) rows, since each row uses two loops of length \(n\).

Explore More Python Number Patterns!

Try more mirror and gap-based patterns to improve your loop control.

All Number Patterns →
Did you know?

Split patterns are a practical way to learn “print value else print space” logic, which is the foundation of many alignment and formatting tasks.

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