Number & Asterisk Diamond Pattern in Python

What You’ll Learn
How to print this number and asterisk diamond pattern in Python:
12*23*3*34*4*4*45*5*5*5*54*4*4*43*3*32*21
The pattern grows to a maximum row, then mirrors back down.
⭐ Pattern Output
For 5 as the peak number, the pattern looks like this:
1
2*2
3*3*3
4*4*4*4
5*5*5*5*5
4*4*4*4
3*3*3
2*2
1Complete 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.
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
Print the increasing half
for i in range(1, 6) prints rows 1 through 5.
Avoid the leading asterisk
count starts at 1. The first print uses print(i, end=""). Later prints add "*"+str(i).
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 *.
Print the decreasing half
for i in range(4, 0, -1) prints 4 down to 1, mirroring the top half.
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²).
Variation — User Input Version
Choose the peak value at runtime and print the pattern up and down.
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
Each row prints the same number repeated i times, separated by *.
A small condition avoids printing a leading separator.
The pattern prints up to a peak, then mirrors back down.
Total work grows quadratically, so runtime is O(n²).
❓ Frequently Asked Questions
" * " + str(i) instead of "*" + str(i), and update your expected output accordingly.i when j == 0, otherwise print "*" + str(i). That avoids an extra variable.Explore More Python Number Patterns!
Mix numbers and symbols to build more interesting practice patterns.
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.
12 people found this page helpful
