Python Program to Print Star Pattern

Python Program to Print Star Pattern

The objective of this Python post, you will see various Python examples that cover the following:

  • Programs for printing pyramid patterns in Python
  • Inverted star pattern in python
  • Program to print inverted half pyramid
  • Python program to print Asterisk pattern
  • Python program to print pyramid
  • Python program to print inverted pyramid pattern

1: Programs for printing pyramid patterns in Python

# Python Program to print star pattern 
  
# Function to demonstrate printing pattern 
def pyramid (n): 
      
    # outer loop to handle number of rows 
    # n in this case 
    for i in range(0, n): 
      
        # inner loop to handle number of columns 
        # values changing acc. to outer loop 
        for j in range(0, i+1): 
          
            # printing stars 
            print("* ",end="") 
       
        # ending line after each row 
        print("\r") 
  
# Driver Code 
n = 5
pyramid(n) 

Output

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

2: Inverted star pattern in python

# python Program print inverted star 

n=11
  

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

Output

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

3: Program to print inverted half pyramid

rows = 6

for i in range (rows,0,-1):
    for j in range(0, i + 1):
        print("*", end=' ')

    print("\r")

Output

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

4: Python program to print Asterisk pattern

rows = 5
for i in range (0, rows):
    for j in range(0, i + 1):
        print("*", end=' ')
    print("\r")

for i in range (rows, 0, -1):
    for j in range(0, i -1):
        print("*", end=' ')
    print("\r")

Output

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

5: Python program to print pyramid

def full_pyramid(rows):
  
    for i in range(rows):
        print(' '*(rows-i-1) + '*'*(2*i+1))

full_pyramid(5)

Output

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

6: Python program to print inverted pyramid pattern

def inverted_pyramid(rows):
   
    for i in reversed(range(rows)):
        print(' '*(rows-i-1) + '*'*(2*i+1))
        
inverted_pyramid(5)

Output

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

Leave a Reply

Your email address will not be published. Required fields are marked *