Shape Rule
Mirror + stars
Row 1 prints 1234554321, row 2 prints 1234**4321, row 5 prints 1********1.

The number & asterisk mirror pattern prints 1..i, a growing ** center, then i..1 — a natural step after odd-length rows in Program 22. This tutorial covers the shape rule, three inner loops, a live preview, algorithm steps, worked Python examples, edge cases, and complexity.
Mirror + stars
Row 1 prints 1234554321, row 2 prints 1234**4321, row 5 prints 1********1.
n..1
for i in range(n, 0, -1) shrinks the digit range each row.
j, k, m
Ascending 1..i, star pairs **, descending i..1.
Same line / next line
Digits and stars use print(..., end=""); end each row with print().
1–9 size
Pick a size n and draw the mirror pattern instantly in the browser.
Complexity
Each row prints O(n) characters; total work scales as n².
A number & asterisk mirror pattern prints ascending digits, a growing star center, then descending digits on each row. With n = 5, the output is 1234554321, 1234**4321, 123****321, 12******21, 1********1.
In Python you use a descending outer loop, three inner loops for j, k, and m, then print() ends each row.
It combines three inner loops with symmetry — a step up from Program 22’s single inner loop.
First inner loop prints ascending digits.
for k in range(i, n) prints star pairs.
Third loop mirrors the left half descending.
Follow Program 22; continue to Program 24 (centered pyramid) next.
In short: for each i from n down to 1, print 1..i, then ** pairs, then i..1, then print().
Given a positive integer n, print a mirror pattern: for each i from n down to 1, print digits 1..i, then (n - i) pairs of **, then digits i..1.
# n = 5 (conceptual shape)
# 1234554321
# 1234**4321
# 123****321
# 12******21
# 1********1 | Item | Type | Description |
|---|---|---|
n | int | Pattern size — outer loop runs from n down to 1. |
j | int | Ascending loop — prints 1..i. |
k | int | Star loop — prints ** for k = i..n-1. |
m | int | Descending loop — prints i..1 to mirror the left. |
for i from n down to 1:
for j from 1 to i:
print j
for k from i to n - 1:
print "**"
for m from i down to 1:
print m
print newline | Approach | Idea | Best for |
|---|---|---|
| Three inner loops | 1234554321, 1234**4321, … | Learning and interviews |
| User-input n | n = int(input(...)) | Flexible console programs |
| Custom fill | "##" or " " instead of "**" | Different center symbols |
| Goal | Pattern |
|---|---|
| Walk rows | for i in range(n, 0, -1) |
| Ascending digits | for j in range(1, i + 1): print(j, end="") |
| Star center | for k in range(i, n): print("**", end="") |
| Descending digits | for m in range(i, 0, -1): print(m, end="") |
| End the row | print() |
| User input | n = int(input(...)) |
Same mirror pattern — different ways to control size and center symbol.
1..iAscending digits in first inner loop
**Star pairs grow as i shrinks
i..1Descending digits mirror the left
3 loopsj ascending, k stars, m descending
Reach for this pattern when teaching symmetry, multiple inner loops, and mixed character output in nested loops.
Natural follow-up after Program 22 — introduces three inner loops and symmetry.
Outer/inner bound practice with an immediate visual check.
Combine loops with input() for a flexible row count.
Compare Program 22 (odd-length rows) and Program 24 (centered pyramid) next.
This is a console teaching pattern — not how you build modern app screens.
Key benefit: one small program that locks in nested loops, output sequencing, and O(n²) thinking.
Choose a size between 1 and 9 and draw the number & asterisk mirror pattern in the browser.
Three complete Python programs — fixed size, user input, and custom center fill. Click View Output to reveal sample console results.
Print five rows of the mirror pattern with three inner loops.
n = 5Hard-coded size — ideal for first demos and screenshots.
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() When i = 5, print 12345, no stars, then 54321 — full mirror with no center fill. When i = 3, print 123, two ** pairs, then 321 — output 123****321. print() after all three inner loops starts the next row.
Read the pattern size with input() instead of hard-coding 5.
Read n with input() and int(); all three loops use n as the bound.
n = int(input("Enter n: "))
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() Same three-loop core as Example 1; only the source of n changes. The star loop bound k < n scales with the user’s input. Non-numeric input raises ValueError from int(input()) — wrap it in try/except in safer labs.
Replace ** with another two-character fill string.
##Keep n = 5 but use hash pairs instead of asterisks in the center.
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() Replace only "**" with "##" in the star loop — digit loops stay the same. Any two-character string works as center fill.
print is built in; use input() when reading input. Set n and loop variables i, j, k, m.
for i in range(n, 0, -1) — each row prints fewer digits and more stars.
for j in range(1, i + 1) then print(j, end="") — left half.
for k in range(i, n) then print("**", end="") — growing center.
for m in range(i, 0, -1) then print(m, end="") — right mirror.
print() ends the row after all three inner loops.
Each row stays symmetric — O(n²) time, O(1) extra memory.
n = 5Trace each outer-loop value of i, star count, and the full row output.
i | Left 1..i | Star pairs | Right i..1 | Row output |
|---|---|---|---|---|
5 | 12345 | 0 | 54321 | 1234554321 |
4 | 1234 | 1 (× **) | 4321 | 1234**4321 |
3 | 123 | 2 | 321 | 123****321 |
2 | 12 | 3 | 21 | 12******21 |
1 | 1 | 4 | 1 | 1********1 |
Star pairs per row = n - i — grows as digits shrink.
Where this tiny pattern (and its loop structure) shows up beyond the homework prompt.
Clearest visual proof that outer and inner bounds interact.
Example: change j <= i and watch the shape change.
Foundation for inverted, pyramid, diamond, and hollow variants.
Example: use (i + j) % 2 for row+column parity grids.
Practice print(..., end="") vs row newline without complex math.
Example: put print() inside the inner loop by mistake.
Swap digits for letters, stars, or spaced output once the loop works.
Example: print j + " " for spaced digits on each row.
Triangular totals make O(n²) concrete for beginners.
Example: count printed digits for n = 10 still → 55.
Pair the pattern with try/except ValueError and positive-n checks.
Example: reject n <= 0 and re-prompt.
Pro Tip: when an interviewer asks for patterns, explain the outer/inner roles first — then write the loops. The story matters as much as the code.
Why this pattern earns a permanent spot in beginner Python courses.
Wrong bounds show up immediately as a broken staircase.
Only loops and console output — no arrays or math libraries.
Invert, center, hollow, or change the fill character with small edits.
Streaming output needs no storage beyond loop counters.
Pro Tip: trace all three inner loops on paper for n = 3 before coding — symmetry bugs hide in loop bounds.
Small habits that keep number-pattern code clean.
Do not skip the descending m loop — without it you lose the mirror.
input()Wrap int(input()) in try/except ValueError so bad input does not crash the script.
print() OutsideOnly call print() after the inner loop finishes the row.
Write each i, star count, and mirror half before coding.
Trace n = 3 on paper before coding larger demos.
Pro Tip: if the output is a vertical list of single digits per line, you almost certainly put print() inside the inner loop.
Mistakes that commonly break mirror number patterns.
Each digit lands on its own line — you get a column, not a triangle.
→ Use print(j, end="") and print("**", end=""); print() only after all three inner loops.
Without for m in range(i, 0, -1) the row is not mirrored.
→ Always print i..1 after the star loop.
Using k <= n prints one extra star pair per row.
→ Use for k in range(i, n) — exactly n - i pairs.
Omitting print() glues every number onto one endless line.
→ Always end the row after the inner loop.
Letters or empty input raise ValueError from int(input()).
→ Wrap int(input()) in try/except ValueError and re-prompt on failure.
Check these inputs before calling the solution done.
Output is just 11 — one digit each side, no stars.
Outer loop never runs — print nothing or show a message.
n < 0Treat as invalid; re-prompt instead of silent empty output.
Output grows as n² characters — fine for labs, noisy for huge n.
int(input()) raises ValueError — validate with try/except first.
Two rows: 1221 and 1**1.
Try these variations to lock in the pattern.
i += 2** with * — slower center growth1..i + right i..1 with star fill — row width stays consistent.print stays on the line; print() advances — mix them carefully.n > 0 for interactive programs; n = 1 prints 11.Quick Takeaway: descending outer loop, three inner loops (j, k, m), then print() after each row.
| Program | Time | Extra space |
|---|---|---|
| Nested loops (Examples 1–2) | O(n²) | O(1) |
| Custom fill (Example 3) | O(n²) | O(1) |
The number & asterisk mirror pattern is a compact lesson in symmetry: print 1..i, star pairs, then i..1 with three inner loops. Master the fixed-n version, then try user input and a custom center fill.
Practice the three examples above, then continue to Program 24 for the centered continuous number pyramid.
Never skip the descending m loop — validate n when reading from the console.
for i in range(n, 0, -1) in the outer loopj, k, m** in the star loop — two chars per iterationint(input()) in try/except ValueError before using nprint() inside any inner loopm loopk <= n in the star loopn in the star bound — hard-code 5 in Example 2 style only for demosn = 1 edge casePrint the pattern the beginner-friendly way.
Mirror + **
Definitionn down to 1
Codej, k, m
Coden - i pairs
ShapeO(n²) time
AnalysisEach 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.
Move on to the centered continuous number pyramid in the Python number-pattern series.
12 people found this page helpful