Python Program to Print D Star pattern

This article shows how to write a Python program to print the Alphabet D star pattern using the for loop, while loop, and functions with an example. 

The below alphabet D star pattern example accepts the user-entered rows and the nested for loop iterates the rows. The Else If or elif condition is to print stars at a few positions, get the Alphabet D pattern of stars, and skip others.

rows = int(input("Enter Alphabet D of Stars Rows = "))
print("====The Alphabet D Star Pattern====")

for i in range(rows):
    print('*', end='')
    for j in range(rows):
        if (i == 0 or i == rows - 1) and j < rows - 1:
            print('*', end='')
        elif i != 0 and i != rows - 1 and j == rows - 1:
            print('*', end='')
        else:
            print(end=' ')
    print()
Enter Alphabet D of Stars Rows = 10
====The Alphabet D Star Pattern====
********** 
*         *
*         *
*         *
*         *
*         *
*         *
*         *
*         *
********** 

The above generates the Alphabet D, but you can also try the code below; it looks better than the above.

rows = int(input("Enter Alphabet D of Stars Rows = "))
print("====The Alphabet D Star Pattern====")

n = rows // 2 + 1
print("*" * n + " " * (rows - 1))

for i in range(rows - 2):
    print("*" + " " * (rows - 2) + "*")
print("*" * n + " " * (rows - 1))
Python Program to print Alphabet D star pattern

Python program to print the Alphabet D Star pattern using while loop

Instead of a For loop, this program uses the while loop to iterate the Alphabetic D pattern rows. Next, it prints the stars at each position. For more Star Pattern programs >> Click Here.

rows = int(input("Enter Alphabet D of Stars Rows = "))

n = rows // 2 + 1
print("*" * n + " " * (rows - 1))

i = 0
while i < (rows - 2):
    print("*" + " " * (rows - 2) + "*")
    i = i + 1

print("*" * n + " " * (rows - 1))
Enter Alphabet D of Stars Rows = 9
*****        
*       *
*       *
*       *
*       *
*       *
*       *
*       *
*****    

In this example, we created a DPattern function that accepts the rows and the symbol or character to print the Alphabet D pattern of the given symbol. It is a slightly modified version of the above D for loop example.

def DPattern(rows, ch):
    print('%c' %ch * (rows - 1))

    for i in range(rows - 2):
        print('%c' %ch + " " * (rows - 2) + '%c' %ch)

    print('%c' %ch * (rows - 1))


row = int(input("Enter Alphabet D of Stars Rows = "))
sy = input("Symbol for D Star Pattern = ")
DPattern(row, sy)
Enter Alphabet D of Stars Rows = 12
Symbol for D Star Pattern = @
@@@@@@@@@@@
@          @
@          @
@          @
@          @
@          @
@          @
@          @
@          @
@          @
@          @
@@@@@@@@@@@