Signup/Sign In

Python Program to print an Inverted Star Pattern

In this tutorial, we will print two different types of inverted patterns which are made up of stars up to n lines. We utilize the built-in function input to get the number of rows in an inverted star pattern from the user (). Because the input() method returns a string value, we must convert the provided number to an integer using the int() function (). Then, using a loop, we create an inverse pyramid pattern.

The patterns are as follows:

  • Inverted Star Pattern

  • Inverted Pyramid Star Pattern

Let us now look one by one to generate the desired pattern:

Inverted Star Pattern

In this pattern, the outer loop is used to print the rows and the inner loop is used to print the column. This program is very basic where users input the size and the compiler prompts the desired pattern.

Algorithm

  1. Take a value from the user and put it into the variable rows.
  2. Use a for loop with a decrease of 1 for each iteration, with a value ranging between n-1 and 0.
  3. Multiply empty spaces by n-a and ‘*' by a then print both.
  4. The pattern of an inverted star is printed.

Program

As of now, we have a rough understanding of how the pattern is printed. Let us now look at the code influenced by the algorithm:

rows = int(input("Enter the number of rows: "))  
 
for a in range(rows + 1, 0, -1):    
    for b in range(0, a - 1):  
        print("*", end=' ')  
    print(" ")


Enter the number of rows: 5
* * * * *
* * * *
* * *
* *
*

Inverted Star Pyramid Pattern

In this pattern, the outer loop is used to print the rows and the inner loop is used to print the column. This program is very basic where users input the size and the compiler prompts the desired pattern. In this program, we are generating inversed pyramid using stars.

Algorithm

  1. Take a value from the user and put it into the variable rows.
  2. Use a for loop with a decrease of 1 for each iteration, with a value ranging between n-1 and 0.
  3. Use another loop with an increase of inner loop with (2*a-1)
  4. Multiply empty spaces by n-a and ‘*' by a then print both.
  5. The pattern of an inverted pyramid star is printed.

Program

As of now, we have a rough understanding of how the pattern is printed. Let us now look at the code influenced by the algorithm:

rows = int(input('Enter the number of rows: '))

for a in range(rows,0,-1):
    for b in range(rows-a):
        print(' ', end='') 
    
    for b in range(2*a-1):
        print('*',end='') 
    print()


Enter the number of rows: 5
*********
*******
*****
***
*

Conclusion

In this tutorial, we have printed inverted star patterns in which the first pattern is a normal star pattern and another one is an inverted pyramid pattern. Both programs are done with help of loops in python programming and are very simple to understand.