This article shows how to write a Python program to print the Alphabet N star pattern using the for loop, while loop, and functions with an example.
The below alphabet N star pattern example accepts the user-entered rows, and the nested for loop iterates over the rows. The If else condition in Python is used to print stars at the first and last columns and the diagonal from the first row to the last to get the Alphabetical N pattern of stars and skip others.
rows = int(input("Enter Alphabet N of Stars Rows = "))
print("====The Alphabet N Star Pattern====")
for i in range(rows):
print("*", end="")
for j in range(rows + 1):
if j == rows or j == i:
print("*", end="")
else:
print(end=" ")
print()
Enter Alphabet N of Stars Rows = 10
====The Alphabet N Star Pattern====
** *
* * *
* * *
* * *
* * *
* * *
* * *
* * *
* * *
* **
This code is another version of writing the Alphabetical N pattern of stars.
rows = int(input("Enter Alphabet N of Stars Rows = "))
print("====The Alphabet N Star Pattern====")
for i in range(rows):
print("*", end="")
for j in range(rows + 1):
if j == rows - 2 or j == i - 1:
print("*", end="")
else:
print(end=" ")
print()

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