Python | Print an Inverted Star Pattern (original) (raw)

Last Updated : 4 Nov, 2025

Given a number n, the task is to print an inverted star pattern where each row prints stars (*) in decreasing order. The pattern starts with n stars in the first row and reduces by one star in each subsequent line, forming an inverted triangle shape.

**For example:

**Input: n = 5
**Output: *****
****
***
**
*

Let’s explore different methods to print an inverted star pattern in Python.

Using for loop

In this method the outer loop controls the number of rows, while each line prints spaces followed by stars. It is the most common and efficient way to print an inverted star pattern.

Python `

n = 5 for i in range(n, 0, -1): print((n - i) * ' ' + i * '*')

`

Output




** *

**Explanation:

Using while loop

One can achieve the same pattern using a while loop for more manual control of iteration. It behaves similarly as for loop but gives you more flexibility in loop control.

Python `

n = 5 i = n while i > 0: print((n - i) * ' ' + i * '*') i -= 1

`

Output




** *

**Explanation:

Using List Comprehension

This method achieves the same output in a single line, making it concise and Pythonic.

Python `

n = 5 [print((n - i) * ' ' + i * '*') for i in range(n, 0, -1)]

`

Output




** *

**Explanation:

Using Recursion

A recursive approach can also generate the inverted pattern by calling the same function repeatedly while reducing the size each time.

Python `

def pattern(n, space=0): if n > 0: print(' ' * space + '*' * n) pattern(n - 1, space + 1)

pattern(5)

`

Output




** *

**Explanation: