This article shows how to write a Python program to print the Alphabet Q star pattern using the for loop, while loop, and functions with an example.
The below alphabet Q star pattern example accepts the user-entered rows, and the nested for loop iterates the rows. The elif condition is to print stars on the four sides and an extra tail to get the Alphabetical Q pattern of stars and skip others.
rows = int(input("Enter Alphabet Q of Stars Rows = "))
print("====The Alphabet Q Star Pattern====")
for i in range(rows):
for j in range(rows):
if (i == 0 or i == rows - 2) and (0 < j < rows - 1):
print("*", end=" ")
elif (j == 0 or j == rows - 1) and (0 < i < rows - 2):
print("*", end=" ")
elif (rows // 2 < i < rows) and (rows // 2 < j == i):
print("*", end=" ")
else:
print(" ", end=" ")
print()
Enter Alphabet Q of Stars Rows = 9
====The Alphabet Q Star Pattern====
* * * * * * *
* *
* *
* *
* *
* * *
* * *
* * * * * * *
*
The above code uses the elif condition to prints the Alphabet Q pattern of stars; the below code use the If else condition is to print stars.
rows = int(input("Enter Alphabet Q of Stars Rows = "))
print("====The Alphabet Q Star Pattern====")
for i in range(rows):
for j in range(rows):
if (i == 0 or i == rows - 2) and (0 < j < rows - 1) or \
(j == 0 or j == rows - 1) and (0 < i < rows - 2) or \
(rows // 2 < i < rows) and (rows // 2 < j == i):
print("*", end=" ")
else:
print(" ", end=" ")
print()

Python program to print the Alphabet Q Star pattern using while loop
Instead of a For loop, this program uses the while loop to iterate the Alphabet Q pattern rows and prints the stars at the required positions. For more Star Pattern programs >> Click Here.
rows = int(input("Enter Alphabet Q of Stars Rows = "))
i = 0
while i < rows:
j = 0
while j < rows:
if (i == 0 or i == rows - 2) and (0 < j < rows - 1):
print("*", end=" ")
elif (j == 0 or j == rows - 1) and (0 < i < rows - 2):
print("*", end=" ")
elif (rows // 2 < i < rows) and (rows // 2 < j == i):
print("*", end=" ")
else:
print(" ", end=" ")
j = j + 1
print()
i = i + 1
Enter Alphabet Q of Stars Rows = 12
* * * * * * * * * *
* *
* *
* *
* *
* *
* *
* * *
* * *
* * *
* * * * * * * * * *
*
In this Program example, we created a QPattern function that accepts the rows and the symbol or character to print the Alphabet Q pattern of the given symbol.
def QPattern(rows, ch):
for i in range(rows):
for j in range(rows):
if (i == 0 or i == rows - 2) and (0 < j < rows - 1):
print('%c ' %ch, end='')
elif (j == 0 or j == rows - 1) and (0 < i < rows - 2):
print('%c' %ch, end='')
elif (rows // 2 < i < rows) and (rows // 2 < j == i):
print('%c' %ch, end='')
else:
print(" ", end=" ")
print()
row = int(input("Enter Alphabet Q of Stars Rows = "))
sy = input("Symbol for Q Star Pattern = ")
QPattern(row, sy)
Enter Alphabet Q of Stars Rows = 14
Symbol for Q Star Pattern = $
$ $ $ $ $ $ $ $ $ $ $ $
$ $
$ $
$ $
$ $
$ $
$ $
$ $
$ $ $
$ $ $
$ $ $
$ $ $
$ $ $ $ $ $ $ $ $ $ $ $
$