π Python Pattern Challenge — Day 17
Pattern printing is a great way to improve your Python loops, spacing, nested loops, and logical thinking. For Day 17, let's create a Hollow Star Diamond Pattern ⭐.
This pattern looks like a diamond from the outside, but the center remains empty. The challenge is to control the left spaces, stars, and inner spaces correctly.
Today's Challenge
Write a Python program to print:
Best and cleanest code will be rewarded! π
Solution 1 — Using Nested for Loops
n = 4 # Upper part for i in range(1, n + 1): print(" " * (n - i), end="") for j in range(2 * i - 1): print("*", end=" ") print() # Middle hollow part for i in range(3): print("* *", end=" ") print(" " * 3, end="") print("* *") # Lower part for i in range(n - 1, 0, -1): print(" " * (n - i), end="") for j in range(2 * i - 1): print("*", end=" ") print()
How it works
The first loop creates the growing upper section:
* * * * * * * * * * * * * * * * Then the middle section keeps the center empty: * * * * * * * * * * * *Finally, the last loop creates the decreasing lower section: * * * * * * * * *
Solution 2 — Using String Multiplication
n = 4 for i in range(1, n + 1): print(" " * (n - i) + "* " * (2 * i - 1)) for i in range(3): print("* *" + " " * 3 + "* *") for i in range(n - 1, 0, -1): print(" " * (n - i) + "* " * (2 * i - 1))
Here:
"* " * (2 * i - 1)controls the number of stars in the upper and lower sections.
And:
" " * 3creates the empty space in the center.
Solution 3 — Using a Function
def star_pattern(n): for i in range(1, n + 1): print(" " * (n - i) + "* " * (2 * i - 1)) for _ in range(3): print("* *" + " " * 3 + "* *") for i in range(n - 1, 0, -1): print(" " * (n - i) + "* " * (2 * i - 1)) star_pattern(4)
Using a function makes the pattern reusable and allows you to experiment with different sizes.
⚡ Short & Clean Code
n = 4 for i in range(1, n + 1): print(" " * (n-i) + "* " * (2*i-1)) for _ in range(3): print("* *" + " " * 3 + "* *") for i in range(n-1, 0, -1): print(" " * (n-i) + "* " * (2*i-1))
π₯ This version keeps the logic simple while producing the complete pattern.
π Challenge Yourself
Can you modify this pattern:
- Make the pattern larger?
- Take the size using input()?
- Create the same pattern using a while loop?
- Make the center gap wider?
- Replace * with another symbol?
- Create a completely hollow version?
Drop your solution below! π
17 Days. 17 Patterns. Stronger Python Logic. ππ₯
Learn • Practice • Grow with CLCODING π

0 Comments:
Post a Comment