Star Pattern in Python

Star pattern in Python | In this article, we will print some shapes of star patterns using the For Loop and While Loop.

We will see these below Python program examples:–

• Square Star Pattern in Python
• Left Half Pyramid Star Pattern in Python
• Right Half Pyramid Star Pattern in Python
• Pyramid Star Pattern in Python
• Reverse Pyramid Star Pattern in Python
• Reverse Left Half Pyramid Star Pattern in Python
• Reverse Right Half Pyramid Star Pattern in Python
• Right Pascal Triangle Star Pattern in Python
• Left Pascal Triangle Star Pattern in Python
• Scalene Triangle Star Pattern in Python
• Diamond Star Pattern in Python
• Hourglass Star Pattern in Python
• Heart Star Pattern in Python
• Hollow Star Pattern in Python

Print Star Pattern in Python using For Loop

In the below pattern programs, we are using the For Loop to print star patterns.

Square Star Pattern in Python

In the below, pattern program the row and column contain N number of stars. The number of rows and columns is equal. So, we can assume the square matrix.

Sample Input/ Output:-

Enter the number of rows: 2
`* * `
`* *`

Enter the number of rows: 3
`* * ** * ** * *`

``````def pattern(n):
for i in range(n):
for j in range(n):
# printing stars
print("* ",end="")
print("\r")

# take inputs
n = int(input('Enter the number of rows: '))

# calling function
pattern(n)``````

Left Half Pyramid Star Pattern in Python

Sample Input/ Output:-

Enter the number of rows: 5
`** ** * ** * * ** * * * *`

``````def pattern(n):
for i in range(n):
for j in range(i+1):
# printing stars
print("*",end=" ")
print("\r")

# take inputs
n = int(input('Enter the number of rows: '))

# calling function
pattern(n)
``````

Right Half Pyramid Star Pattern in Python

Sample Input/ Output:-

``````Enter the number of rows: 7

*
* *
* * *
* * * *
* * * * *
* * * * * *
* * * * * * * ``````
``````def pattern(n):
for i in range(n):
for j in range(n-i-1):
# printing spaces
print(" ", end=" ")

for j in range(i+1):
# printing stars
print("* ",end="")
print()

# take inputs
n = int(input('Enter the number of rows: '))

# calling function
pattern(n)``````

Triangle or Pyramid Star Pattern in Python

Sample Input/ Output:-

``````Enter the number of rows: 8

*
* *
* * *
* * * *
* * * * *
* * * * * *
* * * * * * *
* * * * * * * *``````
``````def pattern(n):
# number of spaces
a = n - 1
for i in range(n):
for j in range(a):
print(end=" ")

# decrementing a after each loop
a = a - 1
for j in range(i+1):
# printing stars
print("* ",end="")
print("\r")

# take inputs
n = int(input('Enter the number of rows: '))

# calling function
pattern(n)``````

Reverse Triangle or Pyramid Star Pattern in Python

Sample Input/ Output:-

``````Enter the number of rows: 5

* * * * * *
* * * * *
* * * *
* * *
* *
* ``````
``````def pattern(n):
# number of spaces
a = (2 * n) - 2
for i in range(n, -1, -1):
for j in range(a, 0, -1):
print(end=" ")

# incrementing a after each loop
a = a + 1
for j in range(0, i+1):
# printing stars
print("* ",end="")
print("\r")

# take inputs
n = int(input('Enter the number of rows: '))

# calling function
pattern(n)
``````

Reverse Left Half Pyramid Star Pattern in Python

Sample Input/ Output:-

Enter the number of rows: 6
`* * * * * *`
`* * * * *`
`* * * *`
`* * *`
`* *`
`*`

``````def pattern(n):
for i in range(n):
for j in range(n-i-1):
# printing stars
print("* ",end="")
print(" ")

# take inputs
n = int(input('Enter the number of rows: '))

# calling function
pattern(n)``````

Reverse Right Half Pyramid Star Pattern in Python

Sample Input/ Output:-

``````Enter the number of rows: 6

* * * * *
* * * *
* * *
* *
* ``````
``````def pattern(n):
for i in range(n-1):
for j in range(i+1):
# printing spaces
print(" ",end=" ")

for k in range(n-i-1):
# printing stars
print("* ",end="")
print()

# take inputs
n = int(input('Enter the number of rows: '))

# calling function
pattern(n)
``````

Right Pascal Triangle Star Pattern in Python

Sample Input/ Output:-

Enter the number of rows: 4
`*`
`* *`
`* * *`
`* * * *`
`* * *`
`* *`
`*`

``````def pattern(n):
# print upper triangle
for i in range(n):
for j in range(i+1):
# printing stars
print("* ",end="")
print("\r")

# print lower triangle
for i in range(n):
for j in range(n-i-1):
# printing stars
print("* ",end="")
print("\r")

# take inputs
n = int(input('Enter the number of rows: '))

# calling function
pattern(n)``````

Left Pascal Triangle Star Pattern in Python

Sample Input/ Output:-

``````Enter the number of rows: 6

*
* *
* * *
* * * *
* * * * *
* * * * * *
* * * * *
* * * *
* * *
* *
* ``````
``````def pattern(n):
# print upper triangle
for i in range(n):
for j in range(n-i-1):
# printing spaces
print(" ", end=" ")

for j in range(i+1):
# printing stars
print("* ",end="")
print()

# print lower triangle
for i in range(n-1):
for j in range(i+1):
# printing spaces
print(" ",end=" ")

for j in range(n-i-1):
# printing stars
print("* ",end="")
print()

# take inputs
n = int(input('Enter the number of rows: '))

# calling function
pattern(n)
``````

Scalene Triangle Star Pattern in Python

Sample Input/ Output:-

``````Enter the number of rows: 5

*
*   *
*   *   *
*   *   *   *
*   *   *   *   *``````
``````def pattern(n):
# number of spaces
a = n - 1
for i in range(n):
for j in range(a):
print(end=" ")

# decrementing a after each loop
a = a - 1
for j in range(i+1):
# printing stars
print("* ",end="  ")
print("\r")

# take inputs
n = int(input('Enter the number of rows: '))

# calling function
pattern(n)
``````

Diamond Star Pattern in Python

Sample Input/ Output:-

``````Enter the number of rows: 7

*
***
*****
*******
*********
***********
*************
***********
*********
*******
*****
***
*``````
``````def pattern(n):
# print upper pyramid
for i in range(n):
for j in range(n-i-1):
print(" ", end="")
for j in range(2*i+1):
# printing stars
print("*", end="")
print()

# print downward pyramid
for i in range(n-1):
for j in range(i+1):
print(" ", end="")
for j in range(2*(n-i-1)-1):
# printing stars
print("*", end="")
print()

# take inputs
n = int(input('Enter the number of rows: '))

# calling function
pattern(n)``````

Hourglass Star Pattern in Python

Sample Input/ Output:-

``````Enter the number of rows: 8

* * * * * * * * * * * * * * *
* * * * * * * * * * * * *
* * * * * * * * * * *
* * * * * * * * *
* * * * * * *
* * * * *
* * *
*
* * *
* * * * *
* * * * * * *
* * * * * * * * *
* * * * * * * * * * *
* * * * * * * * * * * * *
* * * * * * * * * * * * * * * ``````
``````def pattern(n):
# print downward pyramid
for i in range(n-1):
for j in range(i):
print(" ", end=" ")
for j in range(2*(n-i)-1):
# printing stars
print("*", end=" ")
print()

# print upper pyramid
for i in range(n):
for j in range(n-i-1):
print(" ", end=" ")
for j in range(2*i+1):
# printing stars
print("*", end=" ")
print()

# take inputs
n = int(input('Enter the number of rows: '))

# calling function
pattern(n)
``````

Heart Star Pattern in Python

Sample Input/ Output:-

``````Enter the number of rows: 9

* * * *           * * * *
* * * * * *       * * * * * *
* * * * * * * *   * * * * * * * *
* * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * *
* * * * * * * * * * * * *
* * * * * * * * * * *
* * * * * * * * *
* * * * * * *
* * * * *
* * *
*``````
``````def pattern(n):
# print upper side of heart
for i in range(n//2, n, 2):
for j in range(1, n-i ,2):
print(" ", end=" ")
for j in range(1, i+1, 1):
print("*", end=" ")
for j in range(1, n-i+1, 1):
print(" ", end=" ")
for j in range(1, i+1, 1):
print("*", end=" ")
print()

# print upper side of heart
for i in range(n,0,-1):
for j in range(i, n, 1):
print(" ", end=" ")
for j in range(1, i*2, 1):
print("*", end=" ")
print()

# take inputs
n = int(input('Enter the number of rows: '))

# calling function
pattern(n)``````

Star Pattern Programs in Python using While Loop

In the previous program, we have used the For Loop to print star patterns but in this program, we will use the While Loop.

Star Pattern Program 14

Sample Input/ Output:-

Enter the number of rows: 4
`*`
`* *`
`* * *`
`* * * *`

``````# Python star pattern to print left half pyramid

def pattern(n):
i = 1
while i <= n :
j = 1
while j <= i:
# printing stars
print("*", end=" ")
j = j + 1
print()
i = i + 1

# take inputs
n = int(input('Enter the number of rows: '))

# calling function
pattern(n)``````

Star Pattern Program 15

Sample Input/ Output:-

Enter the number of rows: 5
`* * * * *`
`* * * *`
`* * *`
`* *`
`*`

``````# Python star pattern to print downward left half pyramid

def pattern(n):
i = 1
while i <= n :
j = n
while j >= i:
# printing stars
print("*", end=" ")
j = j - 1
print()
i = i + 1

# take inputs
n = int(input('Enter the number of rows: '))

# calling function
pattern(n)``````

Star Pattern Program 16

Sample Input/ Output:-

``````Enter the number of rows: 5

*
* *
* * *
* * * *
* * * * * ``````
``````# Python star pattern to print right half pyramid

def pattern(n):
i = 1
while i<=n:
# printing stars
print("  "*(n-i) + "* " * i)
i+=1

# take inputs
n = int(input('Enter the number of rows: '))

# calling function
pattern(n)``````

Star Pattern Program 17

Sample Input/ Output:-

``````Enter the number of rows: 5

* * * * *
* * * *
* * *
* *
*
``````
``````# Python star pattern to print downward right half pyramid

def pattern(n):
i = n
while i>=1:
# printing stars
print(" "*(n-i) + "*" * i)
i-=1

# take inputs
n = int(input('Enter the number of rows: '))

# calling function
pattern(n)
``````

Star Pattern Program 18

Sample Input/ Output:-

``````Enter the number of rows: 4

*
* *
* * *
* * * *``````
``````# Python star pattern to print triangle

def pattern(n):
i = 1
while i<=n:
# printing stars
print(" "*(n-i) + "* " * i)
i+=1

# take inputs
n = int(input('Enter the number of rows: '))

# calling function
pattern(n)
``````

Star Pattern Program 19

Sample Input/ Output:-

``````Enter the number of rows: 5

* * * * *
* * * *
* * *
* *
*
``````
``````# Python star pattern to print downward triangle

def pattern(n):
i = n
while i>=1:
# printing stars
print(" "*(n-i) + "* " * i)
i-=1

# take inputs
n = int(input('Enter the number of rows: '))

# calling function
pattern(n)
``````

Star Pattern Program 20

Sample Input/ Output:-

Enter the number of rows: 4
`*`
`* *`
`* * *`
`* * * *`
`* * *`
`* *`
`*`

``````# Python star pattern to print right pascal triangle

def pattern(n):
# print upper triangle
i = 1
while i<n:
# printing stars
print("* " * i)
i+=1

# print lower triangle
i = n
while i>=1:
# printing stars
print("* " * i)
i-=1

# take inputs
n = int(input('Enter the number of rows: '))

# calling function
pattern(n)
``````

Star Pattern Program 21

Sample Input/ Output:-

``````Enter the number of rows: 4

*
**
***
****
***
**
*
``````
``````# Python star pattern to print left pascal triangle

def pattern(n):
# print upper triangle
i = 1
while i<n:
# printing stars
print(" "*(n-i) + "*" * i)
i+=1

# print lower triangle
i = n
while i>=1:
# printing stars
print(" "*(n-i) + "*" * i)
i-=1

# take inputs
n = int(input('Enter the number of rows: '))

# calling function
pattern(n)
``````

Star Pattern Program 22

Sample Input/ Output:-

``````Enter the number of rows: 5

*
* *
* * *
* * * *
* * * * *
* * * *
* * *
* *
*
``````
``````# Python program to print diamond star pattern

def pattern(n):
# print upper triangle
i = 1
while i<n:
# printing stars
print(" "*(n-i) + "* " * i)
i+=1

# print lower triangle
i = n
while i>=1:
# printing stars
print(" "*(n-i) + "* " * i)
i-=1

# take inputs
n = int(input('Enter the number of rows: '))

# calling function
pattern(n)
``````

Star Pattern Program 23

Sample Input/ Output:-

``````Enter the number of rows: 5

* * * * *
* * * *
* * *
* *
*
* *
* * *
* * * *
* * * * * ``````
``````# Python program to print hourglass star pattern

def pattern(n):
# print upper triangle
i = n
while i>1:
# printing stars
print(" "*(n-i) + "* " * i)
i-=1

# print lower triangle
i = 1
while i<=n:
# printing stars
print(" "*(n-i) + "* " * i)
i+=1

# take inputs
n = int(input('Enter the number of rows: '))

# calling function
pattern(n)
``````

Hollow Star Pattern in Python

In the below pattern programs, we will print hollow star patterns using the for loop and while loop.

Hollow Square Star Pattern in Python

Sample Input/ Output:-

``````Enter the number of rows: 5

* * * * *
*       *
*       *
*       *
* * * * *``````
``````def pattern(n):
for i in range(n):
for j in range(n):
# printing stars
if i == 0 or i == n-1 or j == 0 or j == n-1:
print("*", end=" ")
else:
print(" ", end=" ")
print("\r")

# take inputs
n = int(input('Enter the number of rows: '))

# calling function
pattern(n)
``````

Hollow Left Half Pyramid Star Pattern in Python

Sample Input/ Output:-

``````Enter the number of rows: 5

*
* *
*   *
*     *
* * * * *``````
``````def pattern(n):
for i in range(1, n+1):
for j in range(i):
# printing stars
if j == 0 or j == i-1:
print("*", end=" ")
else:
if i != n:
print(" ", end=" ")
else:
print("*", end=" ")
print()

# take inputs
n = int(input('Enter the number of rows: '))

# calling function
pattern(n)
``````

Hollow Reverse Left Half Pyramid Star Pattern in Python

Star Pattern Program 26

``````Enter the number of rows: 5

* * * * *
*     *
*   *
* *
* ``````
``````def pattern(n):
for i in range(n, 0, -1):
for j in range(i, 0, -1):
if i == 1 or i == n or j == 1 or j == i:
# printing stars
print("*", end=" ")
else:
print(" ", end=" ")
print()

# take inputs
n = int(input('Enter the number of rows: '))

# calling function
pattern(n)
``````

Hollow Triangle or Pyramid Star Pattern in Python

Sample Input/ Output:-

``````Enter the number of rows: 7

*
*   *
*       *
*           *
*               *
*                   *
* * * * * * * * * * * * *``````
``````def pattern(n):
for i in range(n):
for j in range(n-i-1):
print(" ", end=" ")

for j in range(2*i+1):
# printing stars
if j == 0 or j == 2*i:
print("*", end=" ")
else:
if i == n-1:
print("*", end=" ")
else:
print(" ", end=" ")
print()

# take inputs
n = int(input('Enter the number of rows: '))

# calling function
pattern(n)
``````

Hollow Reverse Triangle or Pyramid Star Pattern in Python

Sample Input/ Output:-

``````Enter the number of rows: 7

* * * * * * * * * * * * *
*                   *
*               *
*           *
*       *
*   *
* ``````
``````def pattern(n):
for i in range(1, n+1):
for j in range(0, i):
print(" ", end=" ")

for j in range(1, (n*2 - (2*i-1)) + 1):
if i == 1 or j == 1 or j ==(n*2 -(2*i-1)):
# printing stars
print("*", end=" ")
else:
print(" ", end=" ")
print()

# take inputs
n = int(input('Enter the number of rows: '))

# calling function
pattern(n)
``````

Hollow Diamond Star Pattern in Python

Sample Input/ Output:-

``````Enter the number of rows: 6

*
*   *
*       *
*           *
*               *
*                   *
*               *
*           *
*       *
*   *
* ``````
``````def pattern(n):
# print upper hollow pyramid
for i in range(n):
for j in range(n-i-1):
print(" ", end=" ")
for j in range(2*i+1):
if j == 0 or j == 2*i:
# printing stars
print("*", end=" ")
else:
print(" ", end=" ")
print()

# print downward hollow pyramid
for i in range(n-1):
for j in range(i+1):
print(" ", end=" ")
for j in range(2*(n-i-1)-1):
if j == 0 or j == 2*(n-i-1)-2:
# printing stars
print("*", end=" ")
else:
print(" ", end=" ")
print()

# take inputs
n = int(input('Enter the number of rows: '))

# calling function
pattern(n)
``````

Hollow Hourglass Star Pattern in Python

Sample Input/ Output:-

``````Enter the number of rows: 5

* * * * * * * * *
*           *
*       *
*   *
*
*   *
*       *
*           *
* * * * * * * * * ``````
``````def pattern(n):
# print upper hollow hourglass
for i in range(n, 0, -1):
for j in range(n-i):
print(" ", end=" ")
for j in range(1, 2*i):
if i==1 or i==n or j==1 or j==2*i-1:
# printing stars
print("*", end=" ")
else:
print(" ", end=" ")
print()

# print lower hollow hourglass
for i in range(2, n+1):
for j in range(n-i):
print(" ", end=" ")
for j in range(1, 2*i):
if i==n or j==1 or j==2*i-1:
# printing stars
print("*", end=" ")
else:
print(" ", end=" ")
print()

# take inputs
n = int(input('Enter the number of rows: '))

# calling function
pattern(n)
``````

Hollow Heart Star Pattern in Python

Sample Input/ Output:-

``````  * *   * *
*     *     *
*           *
*       *
*   *
*  ``````
``````def pattern(n):
for i in range(n):
for j in range(n+1):
if(i==0 and j%3!=0) or (i==1 and j%3==0)
or (i-j==2) or (i+j==8):
# printing stars
print("*", end=" ")
else:
print(" ", end=" ")
print()

# take inputs
n = 6

# calling function
pattern(n)
``````

Python Star Pattern Programs

In the previous programs, we have used the For Loop to print hollow star patterns but in this program, we will use the While Loop.

Star Pattern Program 33

Sample Input/ Output:-

``````Enter the number of rows: 5

*
* *
*   *
*     *
* * * * *
``````
``````# Python star pattern to print hollow left half pyramid

def pattern(n):
i=1
while i<=n:
if i==1:
print("" * (n-i) + "*")
elif i==n:
print("* " * i)
else:
print("" * (n-i) + "*" + " " * (2*i-3) + "*")
i+=1

# take inputs
n = int(input('Enter the number of rows: '))

# calling function
pattern(n)
``````

Star Pattern Program 34

Sample Input/ Output:-

``````Enter the number of rows: 6

*
* *
*   *
*     *
*       *
* * * * * *
``````
``````# Python star pattern to print hollow triangle

def pattern(n):
i=1
while i<=n:
if i==1:
print(" " * (n-i) + "*")
elif i==n:
print("* " * i)
else:
print(" " * (n-i) + "*" + " " * (2*i-3) + "*")
i+=1

# take inputs
n = int(input('Enter the number of rows: '))

# calling function
pattern(n)
``````

Also See:- Pattern Programs in C