Python Number Mirror Pattern (Asterisk Center)

Beginner
5 min read
Updated: Sep 2026
3 programs
Live preview

What Is This Pattern?

A number & asterisk mirror prints ascending digits 1..i, a growing ** center, then descending digits i..1 — three inner loops on each row.

Remember
Rule: for i = n down to 1
      print 1..i, then (n−i)×"**", then i..1

1234554321
1234**4321
123****321
12******21
1********1     ← n = 5

As i shrinks, the star center grows — a natural step after odd-length rows in Program 22.

How to Solve It

Count i down from n; print left digits, then ** pairs, then right digits.

MethodIdeaBest for
Three inner loopsAscend, stars, descendLearning, interviews
input() sizeSame loops; read size at runtimeInteractive practice
Custom fillReplace "**" with "##" etc.Variant practice

Pseudocode

Pseudocode
for i from n down to 1:
    for j from 1 to i:
        print j (same line, no spaces)
    for k from i to n - 1:
        print "**" (same line)
    for m from i down to 1:
        print m (same line, no spaces)
    print newline

Cheat sheet

GoalPattern
Set sizen = 5
Outer loopfor i in range(n, 0, -1):
Left digitsfor j in range(1, i + 1): print(j, end="")
Center starsfor k in range(i, n): print("**", end="")
Right digitsfor m in range(i, 0, -1): print(m, end="")
End rowprint()
Custom fillprint("##", end="") in the center loop

Printing Numbers vs Starting a New Line

APIEffectUse for
print(..., end="")Stays on the same line, no spaceDigits and **
print()Ends the current lineAfter all three inner loops

Glue digits and stars with end="", then break once with print(). Putting print() inside an inner loop splits the mirror across many lines.

Live Preview

Change size n and the mirror pattern updates instantly.

Whole numbers from 1 to 9. Tap a chip or type a value — the preview redraws as you go.

Live result n = 5 · rows = 5
1234554321
1234**4321
123****321
12******21
1********1

Worked Walkthrough — n = 3

Trace each outer value of i, the left digits, star pairs, and right mirror.

iLeft 1..iStarsRight i..1Printed row
3123none321123321
2121× **2112**21
112× **11****1

Star pairs = n − i. Digits shrink as the center grows — every row stays a balanced mirror.

Python Programs

Three complete programs: fixed n = 5, input() size, and a custom center fill. Use View Output for sample results.

Example 1 — Fixed n = 5

Hard-coded size — three inner loops build left digits, **, then right digits.

Python
n = 5

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

How It Works

1. Outer countdown. i runs from n down to 1 — digit half-width for that row.

2. Left then center. Print 1..i, then print "**" exactly n − i times.

3. Right mirror. Print i..1, then print() ends the row.

Example 2 — input() Size

Read size at runtime; the three-loop structure stays the same.

Python
n = int(input("Enter the size: "))

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

How It Works

1. Read size. Convert the prompt answer with int().

2. Same three loops. Center star count still equals n − i for the chosen size.

3. Safer input tip. Bare int(input()) raises ValueError on letters. Prefer:

Safer input
raw = input("Enter the size: ").strip()
try:
    n = int(raw)
except ValueError:
    print("Enter a positive whole number.")
    raise SystemExit(1)
if n < 1:
    print("Enter a positive whole number.")
    raise SystemExit(1)

Example 3 — Custom Fill ##

Keep n = 5 but replace asterisk pairs with hash pairs in the center.

Python
n = 5
fill = "##"

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

How It Works

1. Same structure. Outer and digit loops match Example 1.

2. Swap the fill. The center loop prints fill instead of "**".

3. Keep two characters. A two-character fill keeps the same visual width as **.

Edge Cases & Pitfalls

Check these before calling the solution done.

one *

Single asterisk

Printing "*" instead of "**" grows the center by 1 and breaks the even-width mirror.

print() inside

Broken rows

If print() is inside any inner loop, the mirror splits across lines. Call it only after all three loops.

range end

Wrong star count

Use range(i, n) (runs n − i times). Using range(i, n + 1) adds one extra pair.

n = 1

Single mirror

Output is just 11 — left 1, no stars, right 1.

n ≤ 0

Empty output

The outer loop never runs. Guard interactive input with n >= 1.

input()

Catch ValueError

Letters crash bare int(input()) — wrap in try/except and require n >= 1.

Time and Space Complexity

ProgramTimeExtra space
Examples 1–3O(n²)O(1)

Each of n rows prints about 2n characters of content → O(n²) total work.

Key Takeaways

  • Rule: print 1..i, then (n − i) pairs of **, then i..1.
  • Three loops: left digits, center fill, right mirror.
  • end= vs print(): glue with end=""; print() advances after each row.
  • Complexity: O(n²) for size n.

One line: shrink the digit halves each row, and grow a two-character center fill to keep the mirror balanced.

Frequently Asked Questions

Each iteration prints two asterisks so the center grows by 2 characters per row while keeping the pattern symmetric.
The pattern prints ascending numbers 1..i, then stars, then descending numbers i..1 on the same line.
Because i starts at n and decreases — each row prints fewer digits and more stars in the center.
print(j, end="") stays on the same line with no trailing space. print() ends the current line. Digits and stars use end=""; the row break uses print() after all three inner loops.
Three — ascending digits (j), star pairs (k), and descending digits (m).
Yes. Replace "**" with "##", two spaces, or any two-character fill (see Example 3).
O(n²) for size n because each row prints O(n) characters overall.
Use try/except ValueError around int(input()) so bad input does not crash the script.
The outer loop never runs, so nothing is printed. Validate and prompt again if you want a clear user message.

Did you know?

Each row prints 1..i, then a growing block of ** pairs, then i..1 — three inner loops create a symmetric mirror. As i shrinks, the star block grows to keep row width consistent.

Next: Centered Continuous Pyramid

Continue with the next pattern in the Python number-pattern series.

Program 24 tutorial →

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