Python Program to Print Diamond Star Pattern

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 Program to Print the Diamond Star Pattern using a for loop.

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 Python Program and replaced the Python while loop with the Python nested for loop to print the 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()
Python Program to Print Diamond Star Pattern

In this Python Program, we created a diamondPattern function that accepts the number of rows and symbols to print a diamond pattern. The sign will replace the star in a diamond pattern. For more star symbol programs, visit the Python star pattern programs.

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, refer to the Python Program for Hollow Diamond Star Pattern

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, check out the Python Half Diamond Star Pattern

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, refer to the Python Hollow Half Diamond Star Pattern

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, check out the Python Half Diamond Mirrored Star Pattern

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, refer to the Python Diamond Number Pattern.

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, visit the Python Diamond Alphabets Pattern

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