Open In App
Related Articles

Python | Print an Inverted Star Pattern

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

An inverted star pattern involves printing a series of lines, each consisting of stars (*) that are arranged in a decreasing order. Here we are going to print inverted star patterns of desired sizes in Python

Examples:

1) Below is the inverted star pattern of size n=5 
(Because there are 5 horizontal lines
or rows consist of stars).

*****
****
***
**
*

2) Below is the inverted star pattern of size n=10
(Because there are 5 horizontal lines
or rows consist of stars).

**********
*********
********
*******
******
*****
****
***
**
*

The provided Python 3 code generates an inverted star pattern with variable n representing the number of rows. The code iterates through a loop, decrementing i from n to 1. For each iteration, the print function is used to generate the pattern. It multiplies (n-i) with space (‘ ‘) and i with asterisk (‘*’) to create the appropriate number of spaces before the stars. This leads to an inverted triangle pattern where the number of stars in each row decreases while the spaces before them increase, creating the desired inverted star pattern.

Let’s see Python program to print inverted star pattern: 

Python3

# python 3 code to print inverted star
# pattern
 
# n is the number of rows in which
# star is going to be printed.
n=11
 
# i is going to be enabled to
# range between n-i t 0 with a
# decrement of 1 with each iteration.
# and in print function, for each iteration,
# ” ” is multiplied with n-i and ‘*’ is
# multiplied with i to create correct
# space before of the stars.
for i in range (n, 0, -1):
    print((n-i) * ' ' + i * '*')

                    

Output
***********
 **********
  *********
   ********
    *******
     ******
      *****
       ****
        ***
         **
          *

Time complexity: O(n) for given input n
Auxiliary Space: O(1)

Example2: Using Recursion

Here we will implement the inverted star pattern using Recursion.

Python3

def inverted_star_pattern_recursive(height):
    if height > 0:
        print("*" * height)
        inverted_star_pattern_recursive(height - 1)
 
height = 5
inverted_star_pattern_recursive(height)

                    

Output:

*****
****
***
**
*





Last Updated : 24 Aug, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads