Python Number-Star Diamond Pattern

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

What Is This Pattern?

A number-star diamond grows from 1 to n*n*…*n, then mirrors back down — each row alternates the row number and *.

Remember
Rule: top  i = 1..n, bottom i = n-1..1
        for j from 1 to 2*i-1:
          odd j → print i   even j → print *

1
2*2
3*3*3
4*4*4*4
5*5*5*5*5
4*4*4*4
3*3*3
2*2
1     ← n = 5

Follows the right-aligned descending triangle in Program 30; next is the increasing triangle from 11 in Program 32.

How to Solve It

Two outer loops build the diamond. One shared inner rule: print 2*i−1 characters, alternating digit and star with j % 2.

MethodIdeaBest for
Two halves + modulusTop 1..n, bottom n-1..1; even j → *Learning, interviews
input() heightSame loops; conditional expressionInteractive practice
Compact n = 3Same structure; easier to trace by handPaper walkthrough

Pseudocode

Pseudocode
for i from 1 to n:
    for j from 1 to 2*i - 1:
        if j % 2 == 0: print *
        else: print i
    print newline

for i from n - 1 down to 1:
    for j from 1 to 2*i - 1:
        if j % 2 == 0: print *
        else: print i
    print newline

Cheat sheet

GoalPattern
Top halffor i in range(1, n + 1):
Bottom halffor i in range(n - 1, 0, -1):
Row lengthfor j in range(1, i * 2): → 2*i-1 chars
Alternateprint("*" if j % 2 == 0 else i, end="")
End of rowprint()

Printing Numbers vs Starting a New Line

APIEffectUse for
print(i, end="") / print("*", end="")Stays on the same lineEach digit or star
print()Ends the current lineAfter the inner loop

Glue characters with end="", then break once with print(). Putting print() inside the inner loop prints one character per line.

Live Preview

Change the height and the number-star diamond updates instantly — capped at 7 for readable demos.

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

Live result n = 5 · 41 chars
1
2*2
3*3*3
4*4*4*4
5*5*5*5*5
4*4*4*4
3*3*3
2*2
1

Worked Walkthrough — Top Half n = 5

Trace how each top-half row builds 2*i−1 characters. The bottom half repeats rows 4..1.

iCharsPatternPrints
11i1
23i * i2*2
35i * i * i3*3*3
47i * … * i4*4*4*4
59peak5*5*5*5*5

Top half = n² chars; bottom = (n−1)². Total lines = 2n − 1 → O(n²).

Python Programs

Three complete programs: fixed n = 5, input() height, and compact n = 3. Use View Output for sample results.

Example 1 — Fixed n = 5

Hard-coded height — top half grows, bottom half mirrors, if/else on j % 2.

Python
for i in range(1, 6):
    for j in range(1, i * 2):
        if j % 2 == 0:
            print("*", end="")
        else:
            print(i, end="")
    print()

for i in range(4, 0, -1):
    for j in range(1, i * 2):
        if j % 2 == 0:
            print("*", end="")
        else:
            print(i, end="")
    print()

How It Works

1. Top half grows. i from 1 to 5 builds longer rows up to the peak.

2. Modulus alternates. Odd j prints i; even j prints *.

3. Bottom mirrors. Second loop starts at n - 1 so the peak is not printed twice.

Example 2 — input() Height

Read n at runtime; use a conditional expression for each position.

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

if n < 1:
    print("Please enter a positive integer.")
else:
    for i in range(1, n + 1):
        for j in range(1, i * 2):
            print("*" if j % 2 == 0 else i, end="")
        print()

    for i in range(n - 1, 0, -1):
        for j in range(1, i * 2):
            print("*" if j % 2 == 0 else i, end="")
        print()

How It Works

1. Read and validate. Parse the answer; reject non-positive values before looping.

2. Same core. The conditional expression matches Example 1’s if/else — only n comes from the user.

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

Safer input
raw = input("Enter n: ").strip()
try:
    n = int(raw)
except ValueError:
    print("Please enter a positive integer.")
    raise SystemExit(1)
if n < 1:
    print("Please enter a positive integer.")
    raise SystemExit(1)

Example 3 — Compact n = 3

Same structure with a small peak — easy to confirm the bottom starts at n - 1.

Python
n = 3

for i in range(1, n + 1):
    for j in range(1, i * 2):
        if j % 2 == 0:
            print("*", end="")
        else:
            print(i, end="")
    print()

for i in range(n - 1, 0, -1):
    for j in range(1, i * 2):
        if j % 2 == 0:
            print("*", end="")
        else:
            print(i, end="")
    print()

How It Works

1. Five lines. Peak 3*3*3 appears once; bottom starts at i = 2.

2. Trace on paper. If the bottom loop starts at n, the peak prints twice.

3. Scale up next. Once the small demo is clear, use Examples 1–2 for height 5 or user input.

Edge Cases & Pitfalls

Check these before calling the solution done.

i = n

Doubled peak

Starting the bottom loop at n reprints the widest row. Use range(n - 1, 0, -1).

range bound

Wrong row length

range(1, i * 2 + 1) adds an extra character. Keep range(1, i * 2) for exactly 2*i-1 chars.

% flip

Stars in wrong spots

Even j must print * and odd j must print i — flipping them breaks the pattern.

print() inside

Broken rows

If print() sits inside the inner loop, you get one character per line. Call it only after the loop.

n = 1

Single digit

Output is just 1 — the bottom loop never runs. A good sanity check for input validation.

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)

Top half prints n² characters; bottom prints (n−1)². Total ≈ 2n² → O(n²) time. Only a few loop variables are needed.

Key Takeaways

  • Two halves: top 1..n, bottom n-1..1 so the peak appears once.
  • Modulus rule: odd j → digit i; even j → *.
  • end= vs print(): characters stay on the line; print() advances after each row.
  • Complexity: O(n²) time from both halves; O(1) extra space.

One line: for each half, print 2*i−1 chars alternating i and *, then print().

Frequently Asked Questions

The inner loop runs for j in range(1, i * 2), which prints 1, 3, 5, 7, 9 characters for i = 1..5.
It checks j % 2. Even j prints '*', odd j prints the current row number i.
The first loop builds the top half (i = 1..n). The second mirrors back down (i = n-1..1) to complete the diamond.
Program 30 is a right-aligned descending triangle. Program 31 alternates digits and stars in a symmetric diamond shape.
Replace 5 with n in both outer loops — see Example 2.
O(n²) for height n because total printed characters grow as n² + (n-1)² across both halves.
Use try/except ValueError around int(input()) or check the raw string with .isdigit() before converting so bad input does not crash the script.
Only one row prints — a single 1. The bottom loop never runs.
Yes — print("*" if j % 2 == 0 else i, end="") compacts the if/else logic in Python.

Did you know?

This pattern prints a top half (1..n) and a bottom half (n-1..1). Each row prints 2*i-1 characters, alternating the row number and * using j % 2.

Next: Increasing Triangle from 11

Continue with a triangle that starts counting from 11.

Program 32 tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

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