Diamond star pattern in Python | In the diamond star pattern program, we will discuss how to print two types of pyramid star pattern programs, first normal pyramid, and second downward pyramid.
In the previous article, we had discuss how to print pyramid and downward pyramid star patterns in python. Let’s see how to print diamond of stars in python using for loop and while loop
Example of diamond star pattern:-
*
***
*****
*******
*********
*******
*****
***
*
Diamond Star Pattern in Python using For Loop
In this program, we are run 6 for loops. The three for loops print the pyramid or upper half part of the diamond. Remember, three for loops print the downward pyramid or lower half part of the diamond.
Program description:- Write a program to print diamond star pattern in python using for loop
# Python program to print diamond star pattern using for loop
# take input
n = 4
# printing pyramid
for i in range(n):
for j in range(n-i-1):
# print spaces
print("", end=" ")
for j in range(2*i+1):
# print stars
print("*", end="")
print()
# printing downward pyramid
for i in range(n-1):
for j in range(i+1):
# print spaces
print("", end=" ")
for j in range(2*(n-i-1)-1):
# print stars
print("*", end="")
print()
Output:-
*
***
*****
*******
*****
***
*
In the previous program, inputs are hardcoded in the program but in this program, inputs will be provided by the user.
# Python program to print diamond star pattern using for loop
# input from user
n = int(input('Enter the number of rows: '))
# printing pyramid
for i in range(n):
for j in range(n-i-1):
# print spaces
print("", end=" ")
for j in range(2*i+1):
# print stars
print("*", end="")
print()
# printing downward pyramid
for i in range(n-1):
for j in range(i+1):
# print spaces
print("", end=" ")
for j in range(2*(n-i-1)-1):
# print stars
print("*", end="")
print()
Output:-
Enter the number of rows: 5
*
***
*****
*******
*********
*******
*****
***
*
We can also take the help of a user-defined function to print diamond star pattern in Python. A function is a block of code that performs a specific task.
# Python program to print diamond star pattern using for loop
def pattern(n):
# printing pyramid
for i in range(n):
for j in range(n-i-1):
# print spaces
print("", end=" ")
for j in range(2*i+1):
# print stars
print("*", end="")
print()
# printing downward pyramid
for i in range(n-1):
for j in range(i+1):
# print spaces
print("", end=" ")
for j in range(2*(n-i-1)-1):
# print stars
print("*", end="")
print()
# input from user
n = int(input('Enter the number of rows: '))
# call function
pattern(n)
Python Program to Print Diamond Star Pattern
This python program also performs the same task but with different methods. In this program, we are using only 2 For Loop. This is the shortest method to print diamond star patterns in python.
# Python program to print diamond star pattern
def pattern(n):
# print upper pyramid
for i in range(n):
print(" "*(n-i-1) + "*"*((2*i)+1) )
# print downward pyramid
for i in range(n):
print(" "*(i+1) + "*"*((2*((n-1)-i))-1))
# input from user
n = int(input('Enter the number of rows: '))
# calling function
pattern(n)
Output:-
Enter the number of rows: 6
*
***
*****
*******
*********
***********
*********
*******
*****
***
*
In this program, we will print the diamond star pattern with space between stars.
# Python program to print diamond star pattern using for loop
def pattern(n):
# printing pyramid
for i in range(n):
# print space and star
print(" "*(n-i-1) + "* "*(i+1))
# printing downward pyramid
for i in range(n):
# print space and star
print(" "*(i+1) + "* "*(n-i-1))
# input from user
n = int(input('Enter the number of rows: '))
# calling function
pattern(n)
Output:-
Enter the number of rows: 6
*
* *
* * *
* * * *
* * * * *
* * * * * *
* * * * *
* * * *
* * *
* *
*
Print Diamond Star Pattern in Python using While Loop
In the previous program, we have used the For Loop to print diamond star patterns but in this program, we will use the While Loop.
Program description:- Write a program to print diamond star pattern in python using while loop
# Python program to print diamond star pattern using while loop
def pattern(n):
# printing pyramid
i = 1
while i<n:
# print space and star
print(" "*(n-i) + "* " * i)
i+=1
# printing downward pyramid
i = n
while i>=1:
# print space and star
print(" "*(n-i) + "* " * i)
i-=1
# input from user
n = int(input('Enter the number of rows: '))
# call function
pattern(n)
Output:-
Enter the number of rows: 5
*
* *
* * *
* * * *
* * * * *
* * * *
* * *
* *
*
Also See:- Pattern Programs in C