Pattern programs play a crucial role in coding interviews, helping candidates strengthen their logical thinking and problem-solving skills. They test a programmer’s ability to use loops, conditions, and print statements efficiently.
One of the biggest challenges in pattern programming is reducing time complexity. While most star patterns are implemented using nested loops, a more optimized approach involves using a single loop to generate the same patterns. This method improves efficiency and enhances a coder’s ability to think creatively. To Learn star patterns using two loops checkout this master star patterns using two for loops.
In this blog, we will explore 10 different star pattern programs that can be implemented using a single for loop in Python. Each program includes:
- Problem Statement
- Code with Comments
- Step-by-Step Explanation
- Expected Output
By the end, you’ll have a solid grasp of single-loop star pattern logic, making your Python coding skills stand out. Let’s get started!
Watch This Video Tutorial on Star Patterns in Python
1. Printing a Single Star (*
)
Code
print("*")
Explanation
print("*")
simply prints a single*
on the screen.- The output is:
Output
*
2. Printing 5 Stars in One Line
Code
for i in range(5):
print("*", end=" ")
Explanation
for i in range(5)
- This loop runs 5 times.
i
starts from0
and goes up to4
.
Iteration Breakdown:
- Iteration 1:
i = 0
- Prints:
*
with a space →*
- Iteration 2:
i = 1
- Prints:
*
with a space →* *
- Iteration 3:
i = 2
- Prints:
*
with a space →* * *
- Iteration 4:
i = 3
- Prints:
*
with a space →* * * *
- Iteration 5:
i = 4
- Prints:
*
with a space →* * * * *
Output
* * * * *
Summary
- Each loop prints one
*
followed by a space. - After 5 loops, all
*
are printed in a single line.
3. Printing Stars in 5 Lines (Vertical)
Code
for i in range(5):
print("*")
Explanation
for i in range(5)
- This is a for loop that runs 5 times.
i
starts from0
and goes up to4
.
Iteration Breakdown:
- Iteration 1:
i = 0
- Prints:
*
and moves to the next line.
- Iteration 2:
i = 1
- Prints:
*
and moves to the next line.
- Iteration 3:
i = 2
- Prints:
*
and moves to the next line.
- Iteration 4:
i = 3
- Prints:
*
and moves to the next line.
- Iteration 5:
i = 4
- Prints:
*
and moves to the next line.
Output
*
*
*
*
*
Summary
- Since there is no
end=" "
, the default behavior ofprint()
moves the cursor to the next line after printing each*
. - Hence, all
*
are printed vertically (one per line).
4. Printing a Right-Angled Triangle Star Pattern
Code
for i in range(1, 6):
print("*" * i)
Explanation
for i in range(1, 6)
- This is a for loop that runs 5 times.
i
starts from1
and goes up to5
(sincerange(1, 6)
generates values from1
to5
).
Iteration Breakdown:
- Iteration 1:
i = 1
- Prints:
*
→*
- Iteration 2:
i = 2
- Prints:
**
→**
- Iteration 3:
i = 3
- Prints:
***
→***
- Iteration 4:
i = 4
- Prints:
****
→****
- Iteration 5:
i = 5
- Prints:
*****
→*****
Output
Summary
"*" * i
→ Repeats the*
symboli
times.- As
i
increases, the number of*
increases, forming a right-angled triangle pattern.
5. Printing a Right-Aligned Triangle Star Pattern
Code
n = 5
for i in range(1, n + 1):
print(" " * (n - i) + "*" * i)
Explanation
for i in range(1, n + 1)
- This loop runs 5 times, starting from
1
and going up to5
(sincen + 1 = 6
, butrange()
excludes the upper limit). i
takes values:1, 2, 3, 4, 5
.
Iteration Breakdown:
- Iteration 1:
i = 1
5 - 1 = 4
spaces →" "
*
→ 1 star
- Iteration 2:
i = 25 - 2 = 3
spaces →" "**
→ 2 stars
- Iteration 3:
i = 35 - 3 = 2
spaces →" "***
→ 3 stars
- Iteration 4:
i = 4
5 - 4 = 1
space →" "
****
→ 4 stars
- Iteration 5:
i = 5
5 - 5 = 0
spaces →""
*****
→ 5 stars
Output
Summary
" " * (n - i)
shifts the stars to the right."*" * i
increases the number of stars in each row.- Together, they create a right-aligned triangle pattern.
6. Printing a Reverse Right-Angled Triangle Star Pattern
Code
for i in range(5, 0, -1):
print("*" * i)
Explanation
for i in range(5, 0, -1)
- This loop starts from 5 and goes down to 1.
5
is the starting value,0
is the stopping value (exclusive), and-1
decreases the value by 1 in each iteration.
Iteration Breakdown:
- Iteration 1:
i = 5
- Prints:
*****
- Iteration 2:
i = 4
- Prints:
****
- Iteration 3:
i = 3
- Prints:
***
- Iteration 4:
i = 2
- Prints:
**
- Iteration 5:
i = 1
- Prints:
*
Output
Summary
range(5, 0, -1)
decreasesi
by 1 in each iteration."*" * i
prints the stars, reducing by one each time, creating an inverted triangle pattern.
7. Printing a Right-Aligned Reverse Triangle Star Pattern
Code
n = 5
for i in range(n, 0, -1):
print(" " * (n - i) + "*" * i)
Explanation
for i in range(n, 0, -1)
- This loop starts from
n
(which is 5) and goes down to1
. 0
is the stopping point (exclusive), and-1
decreases the value by 1 in each iteration.
" " * (n - i)
- Adds spaces before the stars.
n - i
calculates the number of leading spaces needed to right-align the pattern.
"*" * i
- Prints
i
stars. - As
i
decreases, the number of stars also decreases, forming an inverted right-aligned pattern.
Iteration Breakdown:
- Iteration 1:
i = 5
n - i = 5 - 5 = 0
spaces →""
*****
→ 5 stars
- Iteration 2:
i = 4
n - i = 5 - 4 = 1
space →" "
****
→ 4 stars
- Iteration 3:
i = 3
n - i = 5 - 3 = 2
spaces →" "
***
→ 3 stars
- Iteration 4:
i = 2
n - i = 5 - 2 = 3
spaces →" "
**
→ 2 stars
- Iteration 5:
i = 1
n - i = 5 - 1 = 4
spaces →" "
*
→ 1 star
Output
Summary
" " * (n - i)
adds leading spaces to shift stars to the right."*" * i
prints decreasing stars in each row.- Together, they create an inverted right-aligned triangle pattern.
8. Creating a Pyramid Star Pattern
Code
n = 5
for i in range(1, n + 1):
print(" " * (n - i) + "*" * (2 * i - 1))
Explanation
for i in range(1, n + 1)
- This loop runs 5 times, starting from
1
to5
(sincen + 1
is6
but excluded inrange()
). i
increases by 1 in each iteration.
" " * (n - i)
- Adds leading spaces to shift the stars to the right.
n - i
calculates the number of spaces required to center-align the stars.
"*" * (2 * i - 1)
- Prints an odd number of stars.
2 * i - 1
increases by 2 with each iteration, printing 1, 3, 5, 7, and 9 stars.
Iteration Breakdown:
- Iteration 1:
i = 1
n - i = 5 - 1 = 4
spaces →" "
2 * 1 - 1 = 1
star →*
- Iteration 2:
i = 2
n - i = 5 - 2 = 3
spaces →" "
2 * 2 - 1 = 3
stars →***
- Iteration 3:
i = 3
n - i = 5 - 3 = 2
spaces →" "
2 * 3 - 1 = 5
stars →*****
- Iteration 4:
i = 4
n - i = 5 - 4 = 1
space →" "
2 * 4 - 1 = 7
stars →*******
- Iteration 5:
i = 5
n - i = 5 - 5 = 0
spaces →""
2 * 5 - 1 = 9
stars →*********
Summary
" " * (n - i)
adds leading spaces to center the stars."*" * (2 * i - 1)
prints increasing stars in an odd pattern.- Together, they create a pyramid pattern.
Output
9. Reversing a Pyramid Star Pattern
Code
n=5
for i in range(n - 1, 0, -1):
print(" " * (n - i) + "*" * (2 * i - 1))
Explaination
for i in range(n - 1, 0, -1)
- This loop starts from
n - 1
(which is4
) and goes down to1
. 0
is the stopping point (exclusive), and-1
decreases the value ofi
by 1 in each iteration.
" " * (n - i)
- Adds leading spaces to center-align the stars.
n - i
calculates the number of spaces required before the stars.
"*" * (2 * i - 1)
- Prints an odd number of stars.
Iteration Breakdown:
- Iteration 1:
i = 4
n - i = 5 - 4 = 1
space →" "
2 * 4 - 1 = 7
stars →*******
- Iteration 2:
i = 3
n - i = 5 - 3 = 2
spaces →" "
2 * 3 - 1 = 5
stars →*****
- Iteration 3:
i = 2
n - i = 5 - 2 = 3
spaces →" "
2 * 2 - 1 = 3
stars →***
- Iteration 4:
i = 1
n - i = 5 - 1 = 4
spaces →" "
2 * 1 - 1 = 1
star →*
Output
Summary
" " * (n - i)
adds spaces to center the stars."*" * (2 * i - 1)
prints decreasing stars in an odd star pattern.- Together, they create an inverted pyramid star pattern.
10. Creating a Diamond Star Pattern
Code
n = 5
for i in range(1, n + 1):
print(" " * (n - i) + "*" * (2 * i - 1))
for i in range(n - 1, 0, -1):
print(" " * (n - i) + "*" * (2 * i - 1))
Explanation
First Loop
for i in range(1, n + 1)
- This loop starts from
1
and goes up to5
. - It prints the top half (pyramid) of the diamond.
- This loop starts from
" " * (n - i)
- Adds spaces before the stars to center them.
"*" * (2 * i - 1)
Iteration Breakdown (First Loop):
i = 1
,n - i = 4
→ 4 spaces + 1 star →*
i = 2
,n - i = 3
→ 3 spaces + 3 stars →***
i = 3
,n - i = 2
→ 2 spaces + 5 stars →*****
i = 4
,n - i = 1
→ 1 space + 7 stars →*******
i = 5
,n - i = 0
→ 0 spaces + 9 stars →*********
Second Loop
for i in range(n - 1, 0, -1)
- This loop starts from
n - 1
(which is4
) and goes down to1
. - It prints the bottom half (inverted pyramid) of the diamond.
- This loop starts from
" " * (n - i)
- Adds spaces before the stars to center them.
"*" * (2 * i - 1)
- Prints an odd number of stars in decreasing order.
Iteration Breakdown (Second Loop):
i = 4
,n - i = 1
→ 1 space + 7 stars →*******
i = 3
,n - i = 2
→ 2 spaces + 5 stars →*****
i = 2
,n - i = 3
→ 3 spaces + 3 stars →***
i = 1
,n - i = 4
→ 4 spaces + 1 star →*
Output
Summary
- The first loop creates the star pattern of top half (pyramid).
- The second loop creates the star pattern of bottom half (inverted pyramid).
- Together, they form a diamond star pattern.
Optimization Tips
- In order to efficiently make star pattern, use string multiplication (*).
- To prevent needless conditions within loops, precompute line breaks.
- Make use of mathematical methods to dynamically calculate the locations of stars and space.
- Reduce code duplication by aiming for succinct logic.
Conclusion
Learning Python star patterns improves one’s capacity for reasoning and problem-solving. Coding efficiency is increased and execution time is optimised when they are implemented in a single loop.
Try out different designs, adjust the reasoning, and practise these patterns! Do you have any queries? Please leave a remark below.
FAQs
1. Why are pattern programs important in Python?
Pattern programs assess a coder’s proficiency with loops and improve reasoning, two skills that are essential in technical interviews.
2. How do I optimize star pattern programs?
Utilise string multiplication, reduce unnecessary computations, and apply mathematical formulae.
3. Can these patterns be implemented using recursion?
Indeed! Recursion is an alternative to loops for solving many patterns.
4. Which Python concepts are used in these programs?
Mathematical reasoning, conditionals, loops, and string manipulation.
5. How can I create custom patterns?
Try out various string operations, spacing, and loop conditions.