Python Program to Print Inverted Pyramid Pattern
You can print an inverted pyramid pattern in Python using nested
for loops where the outer loop controls the rows and the inner loops print spaces and stars; for example: for i in range(n, 0, -1): print(' ' * (n - i) + '* ' * i).Examples
Inputn = 3
Output* * *
* *
*
Inputn = 5
Output* * * * *
* * * *
* * *
* *
*
Inputn = 1
Output*
How to Think About It
To print an inverted pyramid, start from the maximum number of stars in the first row and decrease the count by one each row. Add spaces before the stars to shift the pattern to the right, increasing spaces as stars decrease. Use one loop for rows and two inner loops: one for spaces and one for stars.
Algorithm
1
Get the number of rows (n) from the user or set it.2
For each row from n down to 1:3
Print spaces equal to (n - current row) to shift stars right.4
Print stars equal to the current row number with spaces between them.5
Move to the next line after each row.Code
python
n = 5 for i in range(n, 0, -1): print(' ' * (n - i) + '* ' * i)
Output
* * * * *
* * * *
* * *
* *
*
Dry Run
Let's trace n=3 through the code
1
Start outer loop with i=3
Print 0 spaces and 3 stars: '* * * '
2
Next outer loop with i=2
Print 1 space and 2 stars: ' * * '
3
Next outer loop with i=1
Print 2 spaces and 1 star: ' * '
| i | Spaces | Stars | Output Line |
|---|---|---|---|
| 3 | 0 | 3 | * * * |
| 2 | 1 | 2 | * * |
| 1 | 2 | 1 | * |
Why This Works
Step 1: Outer loop controls rows
The for loop runs from n down to 1, controlling how many stars to print each row.
Step 2: Print spaces to shift stars
Spaces are printed first to move the stars right, increasing as the row number decreases using ' ' * (n - i).
Step 3: Print stars with spaces
Stars are printed with a space after each using '* ' * i to form the pyramid shape.
Alternative Approaches
Using string join with list comprehension
python
n = 5 for i in range(n, 0, -1): print(' ' * (n - i) + ' '.join(['*'] * i))
This method builds the star string with join, which can be clearer and more flexible for customization.
Using while loops
python
n = 5 i = n while i > 0: print(' ' * (n - i) + '* ' * i) i -= 1
Using while loops instead of for loops achieves the same result but is less concise.
Complexity: O(n^2) time, O(1) space
Time Complexity
The program uses nested loops: the outer loop runs n times and the inner printing of stars runs up to n times, resulting in O(n^2) time.
Space Complexity
The program uses only a few variables and prints directly, so space complexity is O(1).
Which Approach is Fastest?
All approaches have similar time complexity; using string join may be slightly more readable but not faster.
| Approach | Time | Space | Best For |
|---|---|---|---|
| Nested for loops | O(n^2) | O(1) | Simple and clear pattern printing |
| String join with list comprehension | O(n^2) | O(1) | Readable and flexible string building |
| While loops | O(n^2) | O(1) | Alternative loop style, less concise |
Use spaces before stars to align the inverted pyramid properly.
Beginners often forget to increase spaces each row, causing the pattern to be left-aligned instead of inverted pyramid shape.