PythonMastery

Inverted triangle pattern in Python

Print an upside-down triangle of stars by counting down with range(rows, 0, -1), plus a right-aligned version, an inverted pyramid and numbers.

An inverted triangle is the star triangle upside down: the first row is the longest, and each row has one star less.

The program

python
rows = int(input("Number of rows: "))

for i in range(rows, 0, -1):
    print("*" * i)

Typed in this example: 5. Press Run to type your own.

output
Number of rows: 5
*****
****
***
**
*

How it works

Other inverted shapes

The same countdown loop prints three more shapes. Each row puts its spaces in front, so no line ends with a space:

python
rows = 5

print("Right-aligned:")
for i in range(rows, 0, -1):
    print(" " * (rows - i) + "*" * i)

print("Inverted pyramid:")
for i in range(rows, 0, -1):
    print(" " * (rows - i) + "*" * (2 * i - 1))

print("Numbers:")
for i in range(rows, 0, -1):
    print(*range(1, i + 1))
output
Right-aligned:
*****
 ****
  ***
   **
    *
Inverted pyramid:
*********
 *******
  *****
   ***
    *
Numbers:
1 2 3 4 5
1 2 3 4
1 2 3
1 2
1

Learn the idea: Loops: for, while, and the Pythonic Patterns, Working with Strings