Python Program to Create Pyramid Star Patterns

18-Oct-2022

.

Admin

Python Program to Create Pyramid Star Patterns

Hi Dev,

If you need to see example of Python Program to Create Pyramid Star Patterns. we will help you to give example of Star Patterns in Python. In this article, we will implement a Pattern Program in Python. this example will help you Learn How To Make Python Pattern Programs With Examples. Follow bellow tutorial step of Star Pattern Programs in Python using For Loop.

See the following python program to print the different types of pattern; as shown below:

so let's see following examples with output:

Example 1: Programs for printing pyramid patterns in Python using Function


# 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 = 6

pyramid(n)

Output:

*

* *

* * *

* * * *

* * * * *

* * * * * *

Example 2: Inverted star pattern in python using For Loop

# python Program print inverted star

n=10

for i in range (n, 0, -1):

print((n-i) * ' ' + i * '*')

Output:

**********

*********

********

*******

******

*****

****

***

**

*

Example 3: Program to print inverted half pyramid

rows = 5

for i in range (rows,0,-1):

for j in range(0, i + 1):

print("*", end=' ')

print("\r")

Output:

* * * * * *

* * * * *

* * * *

* * *

* *

Example 4: Python program to print Asterisk pattern

rows = 6

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:

*

* *

* * *

* * * *

* * * * *

* * * * * *

* * * * *

* * * *

* * *

* *

*

Example 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:

*

***

*****

*******

*********

Example 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:

*********

*******

*****

***

*

I hope it can help you...

#Python