Inverted hollow pyramid star pattern in python

March 17, 2022

Print Inverted Hollow Pyramid Star Pattern

Let’s look at the program for hollow inverted right triangle star pattern in python.

Enter the Number: 7
 *************
  *         *
   *       *
    *     *
     *   *
      * *
       *

Hollow inverted right triangle star pattern in python

Working:

  • Step 1. Start
  • Step 2. Take number of rows as input from the user and stored it into num.
  • Step 3. Run a loop ‘i’ number of times to iterate through all the rows which is Starting from i=0 to num. 
  • Step 4. Run a nested loop inside the main loop for printing spaces which is starting from j=0 to i.
  • Step 5. Run a nested loop inside the main loop for printing stars which is starting from j=num*2 to [num*2 – [2*i – 1]]+1.
  • Step 6. Inside the above loop print stars only if i == 1 or j == 1 or j ==[num*2 -[2*i-1]] in all other cases print a blank space.
  • Step 7. Move to the next line by printing a new line using print[] function.
  • Step 8. Stop

Python Program:

Run

num = int[input["Enter the Number: "]]

for i in range[1, num+1]:
    for j in range[0, i]:
        print[" ", end=""]

    for j in range[1, [num*2 - [2*i - 1]]+1]:
        if i == 1 or j == 1 or j ==[num*2 -[2*i-1]]:
            print["*", end=""]
        else:
            print[" ", end=""]
    print[]

Output

Enter the Number: 7
 *************
  *         *
   *       *
    *     *
     *   *
      * *
       *

Process finished with exit code 0

One comment on “Python Program for Printing Inverted Hollow Pyramid Star Pattern”

  • Khushboo

    n = int[[input[“Enter the number:]]]
    for i in range[n,-1,-1]:
    if [[i==0] or [i==n]]:
    print[” “*[n-i]+ [“*”]*[2*i+1]]
    else:
    print[” “*[n-i] + [“*”] + [” “]*[2*i -1] + [“*”]]

    0

This Python program prints or generates inverted hollow star pyramid pattern using python loop and python decision making statement [if-else].

Python Source Code: Hollow Star Pyramid Pattern


# Generating Inverse Hollow Pyramid Pattern Using Stars

row = int[input['Enter number of rows required: ']]

for i in range[row,0,-1]:
    for j in range[row-i]:
        print[' ', end=''] # printing space and staying in same line
    
    for j in range[2*i-1]:
        if i==0 or i==row or j==2*i-2 or j==0:
            print['*',end=''] # printing * and staying in same line
        else:
            print[' ', end=''] # printing space and staying in same line
    print[] # printing new line

Output

Enter number of rows required: 17

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

So, I have this code

num = int[input["Enter the Number: "]]

for i in range[1, num+1]:
    for j in range[0, i]:
        print[" ", end=""]

    for j in range[1, [num*2 - [2*i - 1]]+1]:
        if i == 1 or j == 1 or j ==[num*2 -[2*i-1]]:
            print["*", end=""]
        else:
            print[" ", end=""]
    print[]

It gives the following output

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

However, I want the following output with spaces on the first line

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

How do I do this? Any help would be appreciated, thanks

In this shot, we will discuss how to generate an inverted hollow left angled triangle using stars in Python.

We can print a plethora of patterns using Python. The only prerequisite to do this is a good understanding of how loops work in Python. Here, we will be using simple for loops to generate an inverted hollow left-angled triangle using stars.

Description

A triangle is said to be left-angled if it has an angle equal to 90 degrees on its right side. An inverted left-angled triangle is just the inverted form of that, with its vertex lying on the bottom.

To execute an inverted left-angled triangle using Python programming, we will be using two for loops:

  • An outer loop: To handle the number of rows.
  • An inner loop: To handle the number of columns.

Code

Let’s look at the code snippet below.

# Number of rows

n = 5

# Loop through rows

for i in range[1,n+1]:

# Loop through columns

for j in range[1, n+1]:

# Printing Pattern

if [j==n] or [i==1] or [i==j]:

print["*", end=" "]

else:

print[" ", end=" "]

print[]

Explanation

  • In line 2, we take the input for the number of rows [i.e., the length of the triangle].

  • In line 5, we create a for loop to iterate through the number of rows.

  • In line 8, we create an inner nested for loop to iterate through the number of columns.

  • From lines 11 to 14, we create the pattern using conditional statements. The end statement helps to stay on the same line.

  • In line 15, the print[] statement is used to move to the next line.

In this way, we generate an inverted hollow left-angled triangle using stars in Python.

CONTRIBUTOR

Vinisha Maheshwari

How do you print a hollow inverted pyramid star pattern in Python?

Hollow inverted right triangle star pattern in python.
Start..
Take number of rows as input from the user and stored it into num..
Run a loop 'i' number of times to iterate through all the rows which is Starting from i=0 to num..
Run a nested loop inside the main loop for printing spaces which is starting from j=0 to i..

How do you make an inverted pyramid in Python?

In this python example, we first read number of row in inverted pyramid star pattern from user using built-in function input[] . Since function input[] returns string value, we need to convert given number to integer type using int[] . And then we generate inverse pyramid pattern using loop.

How do I print inverted number pattern in Python?

Python Program to Print an Inverted Star Pattern.
Take a value from the user and store it in a variable n..
Use a for loop where the value of i ranges between the values of n-1 and 0 with a decrement of 1 with each iteration..
Multiply empty spaces with n-i and '*' with i and print both of them..

Chủ Đề