Introduction
Star pattern programs are commonly asked in coding interviews and are a great way to practice loops and nested loops in Python. They help in understanding control flow, especially how loops work. These programs often involve printing star (*
) symbols in various shapes such as triangles, pyramids, diamonds, and squares. In this post, we will cover 15 popular star pattern programs in Python, along with detailed explanations and code.
1. Right-Angled Triangle Star Pattern
Description:
This pattern forms a right-angled triangle with stars, where the number of stars increases with each row.
Example:
*
**
***
****
*****
Python Program:
rows = 5
for i in range(1, rows + 1):
# Print '*' i times
print('*' * i)
Explanation:
- The outer loop runs from 1 to
rows
. - Each iteration of the loop prints
i
stars on a new line.
2. Inverted Right-Angled Triangle Star Pattern
Description:
This pattern prints an inverted right-angled triangle, starting with the maximum number of stars and decreasing with each row.
Example:
*****
****
***
**
*
Python Program:
rows = 5
for i in range(rows, 0, -1):
# Print '*' i times
print('*' * i)
Explanation:
- The outer loop starts from
rows
and decreases by 1 in each iteration. - Each iteration prints the current row number of stars, forming an inverted triangle.
3. Pyramid Star Pattern
Description:
This pattern prints a pyramid of stars that are centered on each row, increasing the number of stars with each row.
Example:
*
***
*****
*******
*********
Python Program:
rows = 5
for i in range(1, rows + 1):
# Print leading spaces and stars
print(' ' * (rows - i) + '*' * (2 * i - 1))
Explanation:
- The first part of the print statement prints spaces to center the stars.
- The second part prints stars, where the number of stars is
(2 * i - 1)
.
4. Inverted Pyramid Star Pattern
Description:
This pattern prints an inverted pyramid where the stars start wide and reduce in number with each row.
Example:
*********
*******
*****
***
*
Python Program:
rows = 5
for i in range(rows, 0, -1):
# Print leading spaces and stars
print(' ' * (rows - i) + '*' * (2 * i - 1))
Explanation:
- The first part of the print statement prints leading spaces.
- The second part prints stars in decreasing order, starting with the maximum number of stars.
5. Hollow Square Star Pattern
Description:
A hollow square pattern where the stars are printed only at the boundaries, and the inside is left hollow.
Example:
*****
* *
* *
* *
*****
Python Program:
rows = 5
for i in range(rows):
if i == 0 or i == rows - 1:
print('*' * rows)
else:
print('*' + ' ' * (rows - 2) + '*')
Explanation:
- The first and last rows are printed completely filled with stars.
- The middle rows are printed with stars at the boundaries, and spaces in between.
6. Hollow Pyramid Star Pattern
Description:
A hollow pyramid pattern where stars are printed only at the boundary of the pyramid and the inside is hollow.
Example:
*
* *
* *
* *
*********
Python Program:
rows = 5
for i in range(1, rows + 1):
if i == rows:
print('*' * (2 * i - 1))
else:
print(' ' * (rows - i) + '*' + ' ' * (2 * i - 3) + ('*' if i > 1 else ''))
Explanation:
- The last row is completely filled with stars.
- For other rows, stars are printed only at the first and last positions, with spaces in between.
7. Diamond Star Pattern
Description:
A diamond shape is formed by combining a pyramid and an inverted pyramid.
Example:
*
***
*****
*******
*********
*******
*****
***
*
Python Program:
rows = 5
# Upper part
for i in range(1, rows + 1):
print(' ' * (rows - i) + '*' * (2 * i - 1))
# Lower part
for i in range(rows - 1, 0, -1):
print(' ' * (rows - i) + '*' * (2 * i - 1))
Explanation:
- The upper part is a regular pyramid.
- The lower part is an inverted pyramid starting from
rows - 1
.
8. Sandglass Star Pattern
Description:
A sandglass pattern with an inverted pyramid on the top and a regular pyramid on the bottom.
Example:
*********
*******
*****
***
*
***
*****
*******
*********
Python Program:
rows = 5
# Upper part
for i in range(rows, 0, -1):
print(' ' * (rows - i) + '*' * (2 * i - 1))
# Lower part
for i in range(2, rows + 1):
print(' ' * (rows - i) + '*' * (2 * i - 1))
Explanation:
- The upper part prints an inverted pyramid.
- The lower part prints a regular pyramid.
9. Right-Aligned Triangle Star Pattern
Description:
A right-aligned triangle where stars are aligned to the right side of the screen.
Example:
*
**
***
****
*****
Python Program:
rows = 5
for i in range(1, rows + 1):
print(' ' * (rows - i) + '*' * i)
Explanation:
- The print statement prints spaces first, followed by stars to align the triangle to the right.
10. Left-Aligned Triangle Star Pattern
Description:
A left-aligned triangle where stars increase in each row, starting from 1 star.
Example:
*
**
***
****
*****
Python Program:
rows = 5
for i in range(1, rows + 1):
print('*' * i)
Explanation:
- This is the simplest pattern, where each iteration of the loop prints
i
stars on a new line.
11. Hollow Diamond Star Pattern
Description:
A hollow diamond shape where only the boundary stars are printed, and the inside is hollow.
Example:
*
* *
* *
* *
*********
* *
* *
* *
*
Python Program:
rows = 5
# Upper part
for i in range(1, rows + 1):
print(' ' * (rows - i) + '*' + ' ' * (2 * i - 3) + ('*' if i > 1 else ''))
# Lower part
for i in range(rows - 1, 0, -1):
print(' ' * (rows - i) + '*' + ' ' * (2 * i - 3) + ('*' if i > 1 else ''))
Explanation:
- The first and last positions of each row are filled with stars, and the inside remains hollow.
12. Inverted Left-Aligned Triangle Star Pattern
Description:
An inverted left-aligned triangle where the stars start from the maximum number in the first row and decrease in each row.
Example:
*****
****
***
**
*
Python Program:
rows = 5
for i in range(rows, 0, -1):
print('*' * i)
Explanation:
- The loop starts from
rows
and decreases by 1 in each iteration, printing stars on each row.
13. Hollow Inverted Pyramid Star Pattern
Description:
An inverted pyramid where only the boundary stars are printed, and the inside is hollow.
Example:
*********
* *
* *
* *
*
Python Program:
rows = 5
for i in range(rows, 0, -1):
if i == rows:
print('*' * (2 * i - 1))
else:
print(' ' * (rows - i) + '*' + ' ' * (2 * i - 3) + ('*' if i > 1 else ''))
Explanation:
- The first row is completely filled with stars, and the remaining rows have stars only at the boundaries.
14. Cross Star Pattern
Description:
This pattern forms a cross (X
) where stars are placed on both diagonals.
Example:
* *
* *
* *
* *
*
* *
* *
* *
* *
Python Program:
rows = 5
for i in range(1, rows * 2):
for j in range(1, rows * 2):
if j == i or j == (rows * 2 - i):
print('*', end='')
else:
print(' ', end='')
print()
Explanation:
- Stars are printed only when the column index matches the row index or its mirror image, forming a cross.
15. Butterfly Star Pattern
Description:
A butterfly-shaped pattern where stars form wings on both sides.
Example:
* *
** **
*** ***
**** ****
**********
**********
**** ****
*** ***
** **
* *
Python Program:
rows = 5
# Upper part
for i in range(1, rows + 1):
print('*' * i + ' ' * (2 * (rows - i)) + '*' * i)
# Lower part
for i in range(rows, 0, -1):
print('*' * i + ' ' * (2 * (rows - i)) + '*' * i)
Explanation:
- The upper part prints stars in increasing order, and the lower part prints stars in decreasing order, forming a butterfly shape.
Conclusion
These 15 Python star pattern programs cover a wide range of shapes, such as triangles, pyramids, diamonds, and crosses. By practicing these patterns, you can improve your understanding of loops, control flow, and logic in Python. These patterns are frequently asked in coding interviews and are a great way to develop your problem-solving skills.
Comments
Post a Comment
Leave Comment