Python Program for Printing Hollow Pyramid Star Pattern

Print Hollow Pyramid Star Pattern

In this Python Program, we will be discussing about how to write a program to print Hollow Pyramid Star Pattern. In this pattern, there are n number of rows and have to take care first printing a spaces and then printing stars with spaces using while loop with this condition k != (2 * i – 1). So, User have to enter a single value, that will be determine as a number of rows of the pattern. With the help of “Nested For Loop and While Loop” , we will print the Hollow Pyramid Star Pattern.

Python Program for Printing Hollow Pyramid Star Pattern


Step 1. Start

Step 2. Take number of rows as input from the user and stored it into num.

Step 3. Initialized a variable k to zero.

Step 4. Run a loop ‘i’ number of times to iterate through all the rows which is Starting from i=1 to num+1.

Step 5. Run a nested loop inside the main loop for printing spaces which is starting from j=i to num.

Step 6. Run a While untill k != (2 * i – 1) this condition is satisfied, Inside the while loop, Check if k == 0 or k == 2 * i – 2 then, print Stars for this pattern.

Step 7. Else if condition is not satisfied then, print spaces.

Step 8. Increase k by k + 1.

Step 9. Outside the while loop, k variable will re-initialized with zero.

Stop 10. Move to the next line by printing a new line using print() function.

Step 11. Run a for loop outside the main loop for printing stars which is starting from i=0 to 2 * num – 1.

Step 12. Stop

Python Program:

num = int(input("Enter the Number: "))
k = 0
for i in range(1, num+1):
    for j in range(i, num):
        print(" ",end="")
    while k != (2 * i - 1):
        if k == 0 or k == 2 * i - 2:
            print("*", end="")
            print(" ", end="")
        k = k+1
    k = 0

for i in range(0, 2 * num - 1):
    print("*", end="")

# This code is contributed by Shubhanshu Arya (Prepinsta Placement Cell Student)