Number & Asterisk Diamond Pattern in Python

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

What You’ll Learn

How to print this number and asterisk diamond pattern in Python:

  • 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

The pattern grows to a maximum row, then mirrors back down.

⭐ Pattern Output

For 5 as the peak number, the pattern looks like this:

Output
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
1

Complete Python Program

This program prints the increasing half (1..5) and then the decreasing half (4..1). It uses a small flag to avoid printing a leading * on each row.

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

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

🧠 How It Works

1

Print the increasing half

for i in range(1, 6) prints rows 1 through 5.

Upward
2

Avoid the leading asterisk

count starts at 1. The first print uses print(i, end=""). Later prints add "*"+str(i).

Formatting
3

Repeat i exactly i times

for j in range(0, i) runs i times so each row contains i copies of the number separated by *.

Repetition
4

Print the decreasing half

for i in range(4, 0, -1) prints 4 down to 1, mirroring the top half.

Downward
=

Diamond-like output

Total printed numbers in the top half is \(1+2+...+n = n(n+1)/2\). With the mirrored bottom half, runtime remains O(n²).

2

Variation — User Input Version

Choose the peak value at runtime and print the pattern up and down.

Python
peak = int(input("Enter the peak number: "))

for i in range(1, peak + 1):
    for j in range(0, i):
        if j == 0:
            print(i, end="")
        else:
            print("*" + str(i), end="")
    print()

for i in range(peak - 1, 0, -1):
    for j in range(0, i):
        if j == 0:
            print(i, end="")
        else:
            print("*" + str(i), end="")
    print()

💡 Tips for Enhancement

Try These

  • Validate input (reject peak < 1) before printing
  • Replace * with another separator like - or |
  • Center the output by adding leading spaces per row
  • Generate the row with "*".join(...) for cleaner code
  • Print similar diamonds using alphabets instead of numbers

Avoid

  • Printing a leading separator (use a condition for the first item)
  • Forgetting the bottom half (the pattern won’t mirror)
  • Mixing default prints with end="" within a row
  • Assuming input is always valid (wrap int() conversion if needed)

Key Takeaways

1

Each row prints the same number repeated i times, separated by *.

2

A small condition avoids printing a leading separator.

3

The pattern prints up to a peak, then mirrors back down.

4

Total work grows quadratically, so runtime is O(n²).

❓ Frequently Asked Questions

One loop builds the pattern upward (1..peak). The second loop builds it downward (peak-1..1) to mirror the output.
Yes. Print " * " + str(i) instead of "*" + str(i), and update your expected output accordingly.
Yes. Use the inner-loop index: print i when j == 0, otherwise print "*" + str(i). That avoids an extra variable.
O(n²) for peak \(n\), because the total printed elements scale with \(1+2+\dots+n\).

Explore More Python Number Patterns!

Mix numbers and symbols to build more interesting practice patterns.

All Number Patterns →
Did you know?

You can generate each row without condition flags by building a list like [str(i)] * i and joining with "*". That keeps the output identical but the code shorter.

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