Python Star Pattern Programs (Triangle, Pyramid, Diamond)

Short answer
Star patterns in Python are printed row by row with a loop and string repetition: 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.

Quick facts
InputThe number of rows n
OutputA pattern of * characters printed line by line
ApproachLoop over the rows; per row print spaces plus stars, computed from the row number
Time complexityO(n²) characters printed

Approach: how to build star patterns step by step

  1. Decide the number of rows n and loop for i in range(1, n + 1).
  2. Write down, for row i, how many spaces and how many stars you need.
  3. 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

Spaces and stars to print on each row for n rows
PatternSpaces in row iStars in row iRows (i)
Right triangle0i1 to n
Inverted right triangle0in down to 1
Right-aligned trianglen - ii1 to n
Pyramidn - i2 * i - 11 to n
Inverted pyramidn - i2 * i - 1n down to 1
Square0n1 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).

Python
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.

Right triangle with string multiplication
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.

Centred pyramid
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.

Diamond
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.

Hollow square with n = 1 edge case
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.

Pyramid with a size typed by the user
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)
No error, but the first line is empty and the triangle has only n - 1 visible rows.

✓ 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)
No error, but the output is a right-aligned triangle instead of a pyramid, because the width grows by 1 per row instead of 2.

✓ 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 →