How do you reverse a star pattern in python?

  1. Home
  2. Python Programs
  3. 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.

View Discussion

Improve Article

Save Article

  • Read
  • Discuss
  • View Discussion

    Improve Article

    Save Article

    Here we are going to print inverted star pattern of desired sizes. 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].
    
       **********
        *********
         ********
          *******
           ******
            *****
             ****
              ***
               **
                *

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

    Python3

    n=11

    for i in range [n, 0, -1]:

        print[[n-i] * ' ' + i * '*']

    Explanation:

    • The first number of rows is stored in variable n.
    • Then the for loop enables i to range between n-i to 0 with a decrement of 1 with each iteration.
    • After that, for each iteration, ” ” is multiplied with n-i and ‘*’ is multiplied with i to create correct space before of the stars.
    • And finally desired pattern will be printed.

    Output:

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

    Time complexity: O[n] for given input n

    Auxiliary Space: O[1]

    How do you invert a star pattern in Python?

    Explanation: The first number of rows is stored in variable n. Then the for loop enables i to range between n-i to 0 with a decrement of 1 with each iteration. After that, for each iteration, ” ” is multiplied with n-i and '*' is multiplied with i to create correct space before of the stars.

    How do you print a reverse pattern in Python?

    Example -.
    # This is the example of print simple reversed right angle pyramid pattern..
    rows = int[input["Enter the number of rows:"]].
    k = 2 * rows - 2 # It is used for number of spaces..
    for i in range[0, rows]:.
    for j in range[0, k]:.
    print[end=" "].
    k = k - 2 # decrement k value after each iteration..
    for j in range[0, i + 1]:.

    How do you print a star pattern backwards?

    printf["Enter the number of rows = "]; scanf["%u",&rows]; printf["Enter the number of rows = "]; scanf["%u",&rows]; The pattern contains N rows and each row contains N-x + 1 columns [where x is the current row number]. So to print the star inner loop iterate x to row [N is row here].

    What is 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.

    Chủ Đề