Python Star Pattern Programs (Triangle, Pyramid, Diamond)
print("*" * i) gives a right triangle, and print(" " * (n - i) + "*" * (2 * i - 1)) gives a centred pyramid.Pattern programs are a classic way to understand loops and nested loops. Every pattern is just a list of rows. For each row you work out two numbers: how many spaces come first and how many stars follow.
Python lets you skip the inner loop for most patterns, because multiplying a string repeats it: "*" * 3 is "***". Use the nested-loop form when you want to see the mechanics, and the string-multiplication form when you want short code.
| Input | The number of rows n |
|---|---|
| Output | A pattern of * characters printed line by line |
| Approach | Loop over the rows; per row print spaces plus stars, computed from the row number |
| Time complexity | O(n²) characters printed |
Approach: how to build star patterns step by step
- Decide the number of rows
nand loopfor i in range(1, n + 1). - Write down, for row
i, how many spaces and how many stars you need. - Print
" " * spaces + "*" * stars.
For a right triangle, row i has 0 spaces and i stars. For a pyramid, row i has n - i spaces and 2 * i - 1 stars. For an inverted triangle, count the rows downwards with range(n, 0, -1) (see range()).
Formulas for common star patterns
| Pattern | Spaces in row i | Stars in row i | Rows (i) |
|---|---|---|---|
| Right triangle | 0 | i | 1 to n |
| Inverted right triangle | 0 | i | n down to 1 |
| Right-aligned triangle | n - i | i | 1 to n |
| Pyramid | n - i | 2 * i - 1 | 1 to n |
| Inverted pyramid | n - i | 2 * i - 1 | n down to 1 |
| Square | 0 | n | 1 to n |
How the nested loop version works
In the nested version the outer loop picks the row and the inner loop prints one character at a time. print("*", end="") stops print from adding a newline, and an empty print() at the end of each row moves to the next line. It is longer than string multiplication but it shows exactly what is happening, and you need it when stars and spaces follow a rule that is not a simple count (for example a hollow shape).
n = 3
for i in range(1, n + 1):
for j in range(i):
print("*", end="")
print()Variations: diamond and hollow shapes
A diamond is a pyramid followed by an inverted pyramid that skips the widest row. A hollow square prints a full row of stars for the first and last rows, and a star, spaces, then a star for the rows in between. Use " " * (n - 2) for the spaces inside.
Examples you can run
Press Run, then edit the code and run it again.
Right triangle with string multiplication
Row i prints i stars.
n = 5
for i in range(1, n + 1):
print("*" * i)Expected output*
**
***
****
*****
Centred pyramid
Each row has n - i leading spaces and 2 * i - 1 stars, so the shape stays symmetric.
n = 4
for i in range(1, n + 1):
print(" " * (n - i) + "*" * (2 * i - 1))Expected output *
***
*****
*******
Diamond
The upper half is a pyramid and the lower half repeats it in reverse, skipping the middle row so it is not printed twice.
n = 4
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))Expected output *
***
*****
*******
*****
***
*
Hollow square with n = 1 edge case
The first and last row are full. Rows between print the borders only. For n = 1 the first row is also the last, so a single star is printed.
def hollow_square(n):
for i in range(n):
if i == 0 or i == n - 1:
print("*" * n)
else:
print("*" + " " * (n - 2) + "*")
hollow_square(5)
print("---")
hollow_square(1)Expected output*****
* *
* *
* *
*****
---
*
Pyramid with a size typed by the user
The number of rows comes from input(); the pyramid code is the same as before.
n = int(input("Rows: "))
for i in range(1, n + 1):
print(" " * (n - i) + "*" * (2 * i - 1))Expected outputRows: 3
*
***
*****
Common mistakes
Starting the loop at zero for a triangle
✗ Wrong
n = 3
for i in range(n):
print("*" * i)✓ Right
n = 3
for i in range(1, n + 1):
print("*" * i)Start at 1: range(1, n + 1), or print i + 1 stars.
Using the wrong number of stars for a pyramid
✗ Wrong
n = 3
for i in range(1, n + 1):
print(" " * (n - i) + "*" * i)✓ Right
n = 3
for i in range(1, n + 1):
print(" " * (n - i) + "*" * (2 * i - 1))A centred pyramid needs the odd numbers of stars: 2 * i - 1.
Python Star Pattern Programs (Triangle, Pyramid, Diamond): questions people ask
How do I print a triangle of stars in Python?
Loop with for i in range(1, n + 1): and print "*" * i. Each row repeats the star character one more time than the row before.
How do I print stars on the same line in Python?
Use print("*", end=""). The end argument replaces the default newline, so the next print continues on the same line.
Why do pyramids use 2 * i - 1 stars?
Each row of a centred pyramid grows by one star on the left and one on the right, so the width goes 1, 3, 5, 7, which is 2 * i - 1.
Keep learning
Step-by-step lessons:
Python Nested LoopsPython for LoopsPython range() Function
Related:
Python Program to Print a Multiplication TablePython FizzBuzz ProgramPython Program to Transpose a MatrixPython Number Guessing Game Program
More in mini programs:
Python Number Guessing Game ProgramPython Simple Calculator ProgramPython Random Password Generator ProgramPython BMI Calculator ProgramPython Rock Paper Scissors Game ProgramPython Tip Calculator Program (Split the Bill) See all →