Python hollow triangle while loop

The way to get a hollow triangle is to print spaces in a loop. If you observe the output you need, you'll see that except for the top and bottom lines, every line has only 2 asterisks (*). That means you need a logic that handles spaces.

There are several ways to write the logic, such as treating each vertical halves as blocks of fixed length and just varying the position of the star or actually counting the spaces for each line. You can explore the different ways to achieve what you need at your convenience. I'll present one soln.

H = int(input("Enter height of triangle: "))
C = str(input("Character: "))
if len(C) != 1:
    C = "*"

rows = 1
count = 0
while rows < H:
    str = ""
    for i in range(H - rows):
        str += " "
    str += C

    if rows > 1:
        for i in range(2 * rows - 3):
            str += " "
        str += C 

    print(str)
    rows += 1

str = ""
for i in range(2 * H - 1):
    str += C
print(str)

I have made a change about checking the character. You should not allow characters of more than length 1. Otherwise, the spacing will get messed up

These exercises are meant for you to understand the logic and get comfortable with manipulating code, so do try different variations

This is probably not the most optimized solution but, remember that printing is in general slow as it has to interact with a peripheral (monitor), so try to print in bulk whenever possible. This improves the speed

This python program generates hollow pyramid pattern made up of stars up to n lines.

In this python example, we first read number of row in the hollow pyramid pattern from user using built-in function input(). And then we use using python's for loop to print hollow pyramid pattern.

Python Source Code: Pyramid Pattern


# Generating Hollow Pyramid Pattern Using Stars

row = int(input('Enter number of rows required: '))

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

In this program print() only is used to bring control to new lines.

Output: Pyramid Pattern

Enter number of rows required: 12
            *
           * *
          *   *
         *     *
        *       *
       *         *
      *           *
     *             *
    *               *
   *                 *
  *                   *
 ***********************

Python Pattern Programs using While Loop

In this tutorial, we will learn how to print patterns to console using Python While Loop.

Example 1 – Python Program to Print Right Triangle using While Loop

In this example, we will write a Python program to print the following start pattern to console. We shall read the number of rows and print starts as shown below.

Pattern

For an input number of 4, following would be the pattern.

*
* *
* * *
* * * *

Try Online

Python Program

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

i = 1
while i <= n :
    j = 1
    while j <= i:
        print("*", end = " ")
        j += 1
    print()
    i += 1

Inner while loop prints a single row after its complete execution. Outer while loop helps to print n number of rows.

In other words, outer while loop prints the rows, while inner while loop prints columns in each row.

Output

Enter number of rows : 6
*
* *
* * *
* * * *
* * * * *
* * * * * *

Example 2 – Python Program to Print Inverted Right Triangle using While Loop

In this example, we will write a Python program to print the following start pattern to console.

Pattern

For an input number of 4, following would be the pattern.

* * * *
* * *
* *
*

Try Online

Python Program

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

i = 1
while i <= n :
    j = n
    while j >= i:
        print("*", end = " ")
        j -= 1
    print()
    i += 1

Output

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

Example 3 – Python Program to Print Number Pattern using While Loop

In this example, we will write a Python program to print the following pattern to console. We shall read the number of rows and print numbers as shown below.

Pattern

For an input number of 5, following would be the pattern.

1
  2   3
  4   5   6
  7   8   9  10
 11  12  13  14  15

Try Online

Python Program

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

k = 1
i = 1
while i <= n :
    j = 1
    while j <= i:
        print("{:3d}".format(k), end = " ")
        j += 1
        k += 1
    print()
    i += 1

Output

Enter number of rows : 7
  1
  2   3
  4   5   6
  7   8   9  10
 11  12  13  14  15
 16  17  18  19  20  21
 22  23  24  25  26  27  28

Conclusion

In this Python Tutorial, we learned to write Python programs to print different types of patterns using While Loop.

How do you draw a hollow triangle in Python?

But in a hollow triangle you need four stages: write spaces, write one star, write zero or more spaces and write one star again. Furthermore, for the last line, you only need to write stars.

How do you print a hollow right triangle in Python?

Description.
# Loop through rows. for i in range(1, n+1):.
# Loop through columns. for j in range(1, n+1):.
if (i == j) or (j == 1) or (i == n): print("*", end=" ").
else: print(" ", end=" ").

How do you print a hollow full pyramid in Python?

3 comments on “Python Program for Printing Hollow Pyramid Star Pattern”.
Khushboo. n = int(input(“Enter the number:”)) for i in range(n): if ((i==0) or (i==n-1)): ... .
mahesh. can solve even simply. num = int(input(“Enter the Number: “)) for i in range(0,num): ... .
Rahul. num = int(input(“enter number:”)) for i in range(1, num):.

How do you make a while loop pyramid in Python?

Source Code.
The outer for loop iterates from i = rows to i = 1 ..
The first inner for loop prints the spaces required in each row..
The second inner for loop prints the first half of the pyramid (vertically cut), whereas the last inner for loop prints the other half..