Python Program to Print (Generate) Star Pyramid Pattern

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

In this python example, we first read number of row in the pyramid 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 pyramid pattern using python's loop.

Python Source Code: Pyramid Pattern


# Generating 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):
        print('*',end='') # printing * and staying in same line
    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: 8
        *
       ***
      *****
     *******
    *********
   ***********
  *************
 ***************