In this Python program, you will learn to print the X pattern of alphabets using for loop, while loop, and functions. The below example accepts the user-entered rows, and the nested for loop iterates the rows and columns. Next, the if else in this program will print the X-shaped alphabet pattern.
rows = int(input("Enter Rows = "))
a = 65
for i in range(rows):
for j in range(rows):
if i == j or j == rows - 1 - i:
print('%c' % (a + j), end='')
else:
print(' ', end='')
print()
Enter Rows = 13
A M
B L
C K
D J
E I
F H
G
F H
E I
D J
C K
B L
A M
Instead of a For loop, this program uses the while loop to iterate the rows and columns and prints the X pattern of the alphabet at each position. For more alphabet pattern programs >> Click Here.
rows = int(input("Enter Rows = "))
a = 65
i = 0
while i < rows:
j = 0
while j < rows:
if i == j or j == rows - 1 - i:
print('%c' % (a + j), end='')
else:
print(' ', end='')
j = j + 1
print()
i += 1
Enter Rows = 11
A K
B J
C I
D H
E G
F
E G
D H
C I
B J
A K
This Python program allows the user to enter the rows. Next, the XpatternAlphabet function uses nested for loops to print the alphabets on rows and columns of an X pattern.
def XpatternAlphabet(rows):
a = 65
for i in range(rows):
for j in range(rows):
if i == j or j == rows - 1 - i:
print('%c' % (a + j), end='')
else:
print(' ', end='')
print()
n = int(input("Enter Rows = "))
XpatternAlphabet(n)