Python Star Patterns
Python Star Patterns
Right-Angled Triangle
rows = 5
for i in range(1, rows + 1):
print("*" * i)
Output:
*
**
***
****
*****
rows = 5
for i in range(rows, 0, -1):
print("*" * i)
Output:
*****
****
***
**
*
3. Pyramid Pattern
rows = 5
for i in range(1, rows + 1):
print(" " * (rows - i) + "*" * (2 * i - 1))
Output:
*
***
*****
*******
*********
4. Inverted Pyramid
rows = 5
for i in range(rows, 0, -1):
print(" " * (rows - i) + "*" * (2 * i - 1))
Output:
*********
*******
*****
***
*
5. Diamond Pattern
rows = 5
for i in range(1, rows + 1):
print(" " * (rows - i) + "*" * (2 * i - 1))
for i in range(rows - 1, 0, -1):
print(" " * (rows - i) + "*" * (2 * i - 1))
Output:
*
***
*****
*******
*********
*******
*****
***
*
rows = 5
for i in range(1, rows + 1):
print(" " * (rows - i) + "*" * i)
Output:
*
**
***
****
*****
7. Sandglass Pattern
rows = 5
for i in range(rows, 0, -1):
print(" " * (rows - i) + "*" * (2 * i - 1))
for i in range(2, rows + 1):
print(" " * (rows - i) + "*" * (2 * i - 1))
Output:
*********
*******
*****
***
*
***
*****
*******
*********
8. Hollow Pyramid
rows = 5
for i in range(1, rows + 1):
if i == 1 or i == rows:
print(" " * (rows - i) + "*" * (2 * i - 1))
else:
print(" " * (rows - i) + "*" + " " * (2 * i - 3) + "*")
Output:
*
* *
* *
* *
*********
9. Hollow Diamond
rows = 5
for i in range(1, rows + 1):
if i == 1:
print(" " * (rows - i) + "*")
else:
print(" " * (rows - i) + "*" + " " * (2 * i - 3) + "*")
for i in range(rows - 1, 0, -1):
if i == 1:
print(" " * (rows - i) + "*")
else:
print(" " * (rows - i) + "*" + " " * (2 * i - 3) + "*")
Output:
*
* *
* *
* *
* *
* *
* *
* *
*
Output:
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
These patterns cover basic and advanced
levels. They are excellent for learning how
nested loops and string manipulations work in
Python.