Python program to print Pyramid patterns

Learn to print different pyramid patterns in Python.

Python program to print Pyramid patterns

Pyramid Pattern #1

Objective:-

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

You can build this pattern by simply using for loops and range.

Code

n = 5

for i in range(1, n+1):
    print(i*'* ')
for i in range(n-1, 0, -1):
    print(i*'* ')

Output

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

Pyramid Pattern #2

Objective:-

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

You can build a pyramid like this by using center() built-in method and for loops.

Code

for i in range(1, 10, 2):
    x = i*'*'
    print(x.center(10))

Output

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

Subscribe to Pylenin

Don’t miss out on the latest issues. Sign up now to get access to the library of members-only issues.
jamie@example.com
Subscribe