Python Program to Print (Generate) Hour-glass Pattern

This python program generates Hour-glass pattern made up of stars.

In this python example, we first read number of row in the Hour-glass pattern from user using built-in function input(). Since function input() returns string value, we need to convert given number to number type using int(). And then we generate Hour-glass pattern using python's loop. First, we generate upper-half of Hour-glass pattern and then we generate lower half-pattern.

Python Source Code: Hour-glass Pattern


# Hourglass pattern in Python

# Reading number of rows
row = int(input("Enter number of rows: "))

print("Generated Hourglass Pattern is: ")
# Upper-half
for i in range(row, 0, -1):
    for j in range(row-i):
        print(" ", end="")
    for j in range(1, 2*i):
        print("*", end="")
    print()

# Lower-half
for i in range(2, row+1):
    for j in range(row-i):
        print(" ", end="")
    for j in range(1, 2*i):
        print("*", end="")
    print()

Output

Enter number of rows: 8
Generated Hourglass Pattern is: 

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