0
0
PythonProgramBeginner · 2 min read

Python Program to Print Inverted Triangle Pattern

You can print an inverted triangle pattern in Python using nested loops like for i in range(n, 0, -1): print('* ' * i) where n is the number of rows.
📋

Examples

Input3
Output* * * * * *
Input5
Output* * * * * * * * * * * * * * *
Input1
Output*
🧠

How to Think About It

To print an inverted triangle pattern, start from the maximum number of stars in the first row and reduce the count by one in each following row until you reach one star. Use a loop to control the rows and another nested loop or string multiplication to print the stars.
📐

Algorithm

1
Get the number of rows (n) from the user or input.
2
Start a loop from n down to 1 (decreasing).
3
In each iteration, print stars equal to the current loop number.
4
Move to the next line after printing stars for each row.
💻

Code

python
n = int(input('Enter number of rows: '))
for i in range(n, 0, -1):
    print('* ' * i)
Output
Enter number of rows: 5 * * * * * * * * * * * * * * *
🔍

Dry Run

Let's trace the program with input 3 to see how it prints the inverted triangle.

1

Input number of rows

User inputs n = 3

2

First iteration (i=3)

Print '* ' repeated 3 times: '* * * '

3

Second iteration (i=2)

Print '* ' repeated 2 times: '* * '

4

Third iteration (i=1)

Print '* ' repeated 1 time: '* '

Iteration (i)Stars Printed
3* * *
2* *
1*
💡

Why This Works

Step 1: Loop from n down to 1

The outer loop counts down from the total rows to 1, controlling how many stars to print each line.

Step 2: Print stars using string multiplication

For each iteration, printing '* ' multiplied by the current count prints the correct number of stars.

Step 3: Move to next line automatically

The print function adds a newline after each row, so stars appear in a triangle shape.

🔄

Alternative Approaches

Using nested loops
python
n = int(input('Enter number of rows: '))
for i in range(n, 0, -1):
    for j in range(i):
        print('*', end=' ')
    print()
This uses explicit inner loop to print stars one by one, which is more flexible but longer code.
Using while loop
python
n = int(input('Enter number of rows: '))
i = n
while i > 0:
    print('* ' * i)
    i -= 1
This uses a while loop instead of for loop, good for learners familiar with while.

Complexity: O(n^2) time, O(1) space

Time Complexity

The program uses nested printing of stars where the total stars printed is roughly n + (n-1) + ... + 1 = n(n+1)/2, which is O(n^2).

Space Complexity

The program uses constant extra space, only variables for counters; printing does not require extra memory proportional to input.

Which Approach is Fastest?

Using string multiplication is faster and simpler than nested loops because it reduces the number of print calls.

ApproachTimeSpaceBest For
String multiplicationO(n^2)O(1)Simple and fast printing
Nested loopsO(n^2)O(1)More control over printing format
While loopO(n^2)O(1)Alternative loop style for beginners
💡
Use string multiplication like '* ' * count to print repeated characters easily.
⚠️
Beginners often forget to decrease the star count each row, causing a normal triangle instead of inverted.