Python Program to Print Hollow Triangle Star Pattern

This article shows how to write a Python Program to Print a Hollow Triangle Star Pattern using a for loop, while loop, and functions. This example uses the for loop and if else condition to iterate and print the Triangle Star Pattern.

rows = int(input("Enter Hollow Triangle Pattern Rows = "))

print("====Hollow Triangle Star Pattern====")

for i in range(1, rows + 1):
    for j in range(0, rows - i):
        print(' ', end = '')
    for k in range(0, i * 2 - 1):
        if i == 1 or i == rows or k == 0 or k == i * 2 - 2:
            print('*', end = '')
        else:
            print(' ', end = '')       
    print()

Output.

Python Program to Print Hollow Triangle Star Pattern using for loop

This Python program replaces the above for loop with a while loop to print the Hollow Triangle Star Pattern.

rows = int(input("Enter Hollow Triangle Pattern Rows = "))

print("====Hollow Triangle Star Pattern====")

i = 1
while i <= rows:
    j = 0
    while j < rows - i:
        print(' ', end = '')
        j = j + 1
    k = 0
    while k < i * 2 - 1:
        if i == 1 or i == rows or k == 0 or k == i * 2 - 2:
            print('*', end = '')
        else:
            print(' ', end = '')
        k  = k  + 1
    i = i + 1
    print()

Output

Enter Hollow Triangle Pattern Rows = 13
====Hollow Triangle Star Pattern====
            *
           * *
          *   *
         *     *
        *       *
       *         *
      *           *
     *             *
    *               *
   *                 *
  *                   *
 *                     *
*************************

In this Python example, we created a HolIowTriangle function that accepts the rows and characters to Print the Hollow Triangle Star Pattern. It replaces the star in the Hollow Triangle pattern with a given symbol.

def HolIowTriangle(rows, ch):
    for i in range(1, rows + 1):
        for j in range(0, rows - i):
            print(' ', end = '')
        for k in range(0, i * 2 - 1):
            if i == 1 or i == rows or k == 0 or k == i * 2 - 2:
                print('%c' %ch, end = '')
            else:
                print(' ', end = '')       
        print()


rows = int(input("Enter Hollow Triangle Pattern Rows = "))
ch = input("Symbol in Hollow Triangle = ")

print("====Hollow Triangle Star Pattern====")
HolIowTriangle(rows, ch)

Output

Enter Hollow Triangle Pattern Rows = 15
Symbol in Hollow Triangle = #
====Hollow Triangle Star Pattern====
              #
             # #
            #   #
           #     #
          #       #
         #         #
        #           #
       #             #
      #               #
     #                 #
    #                   #
   #                     #
  #                       #
 #                         #
#############################