This article explains all the different ways of writing Python Program to Print the Diamond Pattern of stars, alphabets, and numbers using the for loop, while loop, and functions.
Python Program to Print Diamond Star Pattern
Write a Python Program to Print the Diamond Star Pattern using a for loop.
# Python Program to Print Diamond Star Pattern rows = int(input("Enter Diamond Pattern Rows = ")) print("Diamond Star Pattern") k = 0 for i in range(1, rows + 1): for j in range(1, rows - i + 1): print(end = ' ') while k != 2 * i - 1: print('*', end = '') k = k + 1 k = 0 print() k = 2 l = 1 for i in range(1, rows): for j in range(1, k): print(end = ' ') k = k + 1 while l <= (2 * (rows - i) - 1): print('*', end = '') l = l + 1 l = 1 print()
Enter Diamond Pattern Rows = 8
Diamond Star Pattern
*
***
*****
*******
*********
***********
*************
***************
*************
***********
*********
*******
*****
***
*
>>>
We altered the above Program and replaced the while loop with the for loop to Print the Diamond Star Pattern.
# Python Program to Print Diamond Star Pattern rows = int(input("Enter Diamond Pattern Rows = ")) print("Diamond Star Pattern") for i in range(1, rows + 1): for j in range(1, rows - i + 1): print(end = ' ') for k in range(0, 2 * i - 1): print('*', end = '') print() for i in range(1, rows): for j in range(1, i + 1): print(end = ' ') for l in range(1, (2 * (rows - i) )): print('*', end = '') print()
In this Python Program, we created a diamondPattern function that accepts the rows and symbols to Print Diamond Pattern. The sign will replace the star in a diamond pattern.
# Python Program to Print Diamond Star Pattern def diamondPattern(rows, ch): for i in range(1, rows + 1): for j in range(1, rows - i + 1): print(end = ' ') for k in range(0, 2 * i - 1): print('%c' %ch, end = '') print() for i in range(1, rows): for j in range(1, i + 1): print(end = ' ') for k in range(1, (2 * (rows - i))): print('%c' %ch, end = '') print() rows = int(input("Enter Diamond Pattern Rows = ")) ch = input("Symbol to Print in Diamond Pattern = ") print("Diamond Pattern") diamondPattern(rows, ch)
Enter Diamond Pattern Rows = 10
Symbol to Print in Diamond Pattern = ^
Diamond Pattern
^
^^^
^^^^^
^^^^^^^
^^^^^^^^^
^^^^^^^^^^^
^^^^^^^^^^^^^
^^^^^^^^^^^^^^^
^^^^^^^^^^^^^^^^^
^^^^^^^^^^^^^^^^^^^
^^^^^^^^^^^^^^^^^
^^^^^^^^^^^^^^^
^^^^^^^^^^^^^
^^^^^^^^^^^
^^^^^^^^^
^^^^^^^
^^^^^
^^^
^
>>>
For the While loop and Functions example of all the below pattern examples, you have to click the hyperlink provided under each section. As it is an overview, we have provided the best way to write the program for the diamond pattern. However, the explanation part remains on individual posts.
Hollow Diamond Star Pattern
For more information >> Click Here
def loopLogic(i, r): for j in range(1, r - i + 1): print(end=' ') for k in range(1, 2 * i): if k == 1 or k == i * 2 - 1: print('*', end='') else: print(' ', end='') print() r = int(input("Enter Rows = ")) for i in range(1, r + 1): loopLogic(i, r) for i in range(r - 1, 0, -1): loopLogic(i, r)
Output
Enter Rows = 7
*
* *
* *
* *
* *
* *
* *
* *
* *
* *
* *
* *
*
Half Diamond Star Pattern
For more information >> Click Here
r = int(input("Enter Rows = ")) for i in range(r): for j in range(0, i + 1): print('*', end = '') print() for i in range(1, r): for j in range(i, r): print('*', end = '') print()
Output
Enter Rows = 6
*
**
***
****
*****
******
*****
****
***
**
*
Hollow Half Diamond Star Pattern
For more information >> Click Here
def loopLogic(i, r): for j in range(0, i + 1): if i == j or j == 0: print('*', end='') else: print(' ', end='') print() r = int(input("Enter Rows = ")) for i in range(r): loopLogic(i, r) for i in range(r -2, -1, -1): loopLogic(i, r)
Output
Enter Rows = 5
*
**
* *
* *
* *
* *
* *
**
*
Half Diamond Mirrored Star Pattern
For more information >> Click Here
def loopLogic(i, r): for j in range(0, r - i): print(' ', end='') for k in range(0, i): print('*', end='') print() r = int(input("Enter Rows = ")) for i in range(r): loopLogic(i, r) for i in range(r, 0, -1): loopLogic(i, r)
Output
Enter Rows = 8
*
**
***
****
*****
******
*******
********
*******
******
*****
****
***
**
*
Python Program to Print Diamond Number Pattern
All the above examples will print the diamond pattern of stars, whereas this one prints the numbers. For more information >> Click Here.
def loopLogic(i, r): for j in range(1, r - i + 1): print(end=' ') for k in range(1, (2 * i)): print(k, end='') print() r = int(input("Enter Rows = ")) for i in range(1, r + 1): loopLogic(i, r) for i in range(r - 1, 0, -1): loopLogic(i, r)
Output
Enter Rows = 5
1
123
12345
1234567
123456789
1234567
12345
123
1
Python Program to Print Diamond Alphabets Pattern
For more information >> Click Here
def loopLogic(i, r): alp = 64 for j in range(1, r - i + 1): print(end =' ') for k in range(1, (2 * i)): print('%c' %(alp + k), end = '') print() r = int(input("Enter Rows = ")) for i in range(1, r + 1): loopLogic(i, r) for i in range(r - 1, 0, -1): loopLogic(i, r)
Output
Enter Rows = 7
A
ABC
ABCDE
ABCDEFG
ABCDEFGHI
ABCDEFGHIJK
ABCDEFGHIJKLM
ABCDEFGHIJK
ABCDEFGHI
ABCDEFG
ABCDE
ABC
A