This article shows how to write a Python Program to Print a Hollow Inverted 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 Hollow inverted Triangle Star Pattern.
rows = int(input("Enter Hollow Inverted Triangle Pattern Rows = ")) print("====Hollow Inverted Triangle Star Pattern====") for i in range(rows, 0, -1): for j in range(1, rows - i + 1): print(' ', end = '') for k in range(1, i * 2): if i == 1 or i == rows or k == 1 or k == i * 2 - 1: print('*', end = '') else: print(' ', end = '') print()
Output.
This Python program replaces the above for loop with a while loop to print the Hollow Inverted Triangle Star Pattern.
rows = int(input("Enter Hollow Inverted Triangle Pattern Rows = ")) print("====Hollow Inverted Triangle Star Pattern====") i = rows while i > 0: j = 1 while j < rows - i: print(' ', end = '') j = j + 1 k = 1 while k < i * 2: if i == 1 or i == rows or k == 1 or k == i * 2 - 1: print('*', end = '') else: print(' ', end = '') k = k + 1 i = i - 1 print()
Output
Enter Hollow Inverted Triangle Pattern Rows = 12
====Hollow Inverted Triangle Star Pattern====
***********************
* *
* *
* *
* *
* *
* *
* *
* *
* *
* *
*
In this Python example, we created a HolIowInvTriangle function that accepts the rows and characters to Print the Hollow Inverted Triangle Star Pattern. It replaces the star in the Hollow Inverted Triangle pattern with a given symbol.
def HolIowInvTriangle(rows, ch): for i in range(rows, 0, -1): for j in range(1, rows - i + 1): print(' ', end = '') for k in range(1, i * 2): if i == 1 or i == rows or k == 1 or k == i * 2 - 1: print('%c' %ch, end = '') else: print(' ', end = '') print() rows = int(input("Enter Hollow Inverted Triangle Pattern Rows = ")) ch = input("Symbol in Hollow Inverted Mirrored Right Triangle = ") HolIowInvTriangle(rows, ch)
Output
Enter Hollow Inverted Triangle Pattern Rows = 14
Symbol in Hollow Inverted Mirrored Right Triangle = $
$$$$$$$$$$$$$$$$$$$$$$$$$$$
$ $
$ $
$ $
$ $
$ $
$ $
$ $
$ $
$ $
$ $
$ $
$ $
$