Star pattern programs in Python using for loop | Here, we will develop a python program to print star patterns. A star pattern is a pattern that consists of a series of stars that create some sort of shape. Some shapes of star patterns are given. We will use the For Loop to display star patterns.
Note:- The first loop is used to display rows and the second loop is used for columns. We will use i, j as loop iterator variables (i => row, j => column).
Star Pattern in Python using For Loop
Star Pattern Program-1
In the program, we will print square star patterns. Each column contains N number of stars where N is the number of rows.
Examples Output:-
Enter the number of rows: 4* * * *
* * * *
* * * ** * * *
Enter the number of rows: 5* * * * *
* * * * *
* * * * *
* * * * *
* * * * *
# Python Program to print star pattern using for loop
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)
Short Methods
# Python Program to print star pattern using for loop
def pattern(n):
for i in range(n):
# printing stars
print("* " * n)
# take inputs
n = int(input('Enter the number of rows: '))
# calling function
pattern(n)
Star Pattern Program-2
In this program, we will develop a python program to print the left half pyramid using two For Loops.
Examples Output:-
Enter the number of rows: 5*
* *
* * *
* * * *
* * * * *
# Python program to print left half pyramid star pattern
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)
Examples Output:-
# Python program to print left half pyramid star pattern
def pattern(n):
for i in range(1, n+1):
# printing stars
print("* " * i)
# take inputs
n = int(input('Enter the number of rows: '))
# calling function
pattern(n)
Star Pattern Program-3
Examples Output:-
Enter the number of rows: 4
*
* *
* * *
* * * *
# Python program to print right half pyramid star pattern
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)
Short Methods
# Python program to print right half pyramid star pattern
def pattern(n):
for i in range(1, n+1):
# printing stars
print(" " * (n-i) + "*" * i)
# take inputs
n = int(input('Enter the number of rows: '))
# calling function
pattern(n)
Star Pattern Program-4
In this program, print a triangle star pattern using for loop. Here we are running 3 for loops, out of which the first for loop is for looping of the column and the other 2 for loops (the sub loops) for looping of the row.
Examples Output:-
Enter the number of rows: 7
*
* *
* * *
* * * *
* * * * *
* * * * * *
* * * * * * *
# Python program to print pyramid star pattern
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)
Short Methods
# Python program to print triangle star pattern
def pattern(n):
for i in range(n):
# printing stars
print(" " * (n-i-1) + "*" * (2*i+1))
# take inputs
n = int(input('Enter the number of rows: '))
# calling function
pattern(n)
Star Pattern Program-5
We will run 3 nested loops, out of which the first for loop is for looping of the column and the other 2 for loops (the sub loops) for looping of the row.
Examples Output:-
Enter the number of rows: 5
* * * * * *
* * * * *
* * * *
* * *
* *
*
# Python program to print reverse pyramid star pattern
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)
Star Pattern Program-6
Examples Output:-
Enter the number of rows: 8
* * * * * * * *
* * * * * * *
* * * * * *
* * * * *
* * * *
* * *
* *
*
# Python program to print reverse left half pyramid star pattern
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)
Short Methods
# Python program to print reverse left half pyramid star pattern
def pattern(n):
for i in range(n):
# printing stars
print("* " * (n-i))
# take inputs
n = int(input('Enter the number of rows: '))
# calling function
pattern(n)
Star Pattern Program-7
Examples Output:-
Enter the number of rows: 5
* * * *
* * *
* *
*
# Python program to print reverse right half pyramid star pattern
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)
Star Pattern Program-8
Examples Output:-
Enter the number of rows: 4
*
* *
* * *
* * * *
* * *
* *
*
# Python program to print right pascal triangle star pattern
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)
Simple Methods
# Python program to print right pascal triangle star pattern
def pattern(n):
for i in range(n):
print('* ' * (i + 1))
for i in range(n):
print('* ' * (n - i - 1))
# take inputs
n = int(input('Enter the number of rows: '))
# calling function
pattern(n)
Star Pattern Program-9
Examples Output:-
Enter the number of rows: 4
*
* *
* * *
* * * *
* * *
* *
*
# Python program to print left pascal triangle star pattern
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)
Star Pattern Program-10
In the below pattern program, we will print a scalene triangle. If none of the three sides of a triangle are equal to each other, it is called a scalene triangle.
Examples Output:-
Enter the number of rows: 8
*
* *
* * *
* * * *
* * * * *
* * * * * *
* * * * * * *
* * * * * * * *
# Python program to print scalene triangle star pattern
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)
Star Pattern Program-11
Examples Output:-
Enter the number of rows: 5
*
***
*****
*******
*********
*******
*****
***
*
# Python program to print diamond star pattern
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)
Star Pattern Program-12
Examples Output:-
Enter the number of rows: 7
* * * * * * * * * * * * *
* * * * * * * * * * *
* * * * * * * * *
* * * * * * *
* * * * *
* * *
*
* * *
* * * * *
* * * * * * *
* * * * * * * * *
* * * * * * * * * * *
* * * * * * * * * * * * *
# Python program to print hourglass star pattern
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)
Star Pattern Program-13
Examples Output:-
Enter the number of rows: 9
* * * * * * * *
* * * * * * * * * * * *
* * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * *
* * * * * * * * * * * * *
* * * * * * * * * * *
* * * * * * * * *
* * * * * * *
* * * * *
* * *
*
# Python program to print heart star pattern
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 For Loop
In the below pattern programs, we will print the hollow square pattern using for loop.
Star Pattern Program-14
Examples Output:-
Enter the number of rows: 6
* * * * * *
* *
* *
* *
* *
* * * * * *
# Python Program to print hollow star pattern
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)
Star Pattern Program-15
Examples Output:-
Enter the number of rows: 6
*
* *
* *
* *
* *
* * * * * *
# Python star pattern to print hollow left half pyramid
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)
Star Pattern Program-16
Examples Output:-
Enter the number of rows: 6
* * * * * *
* *
* *
* *
* *
*
# Python star pattern to print hollow downward left half pyramid
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)
Star Pattern Program-17
Examples Output:-
Enter the number of rows: 5
*
* *
* *
* *
* * * * * * * * *
# Python program to print hollow pyramid star pattern
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)
Star Pattern Program-18
Examples Output:-
Enter the number of rows: 5
* * * * * * * * *
* *
* *
* *
*
# Python program to print hollow reverse pyramid star pattern
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)
Star Pattern Program-19
Examples Output:-
Enter the number of rows: 5
*
* *
* *
* *
* *
* *
* *
* *
*
# Python program to print hollow diamond star pattern
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)
Star Pattern Program-20
Examples Output:-
Enter the number of rows: 6
* * * * * * * * * * *
* *
* *
* *
* *
*
* *
* *
* *
* *
* * * * * * * * * * *
# Python program to print hollow hourglass star pattern
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)
Star Pattern Program-21
Examples Output:-
* * * *
* * *
* *
* *
* *
*
# Python program to print hollow heart star pattern
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)
Also See:- Pattern Programs in C