Alphabet pattern programs in Python
In this post, you will learn how to print various alphabetic patterns using the Python programming language.
An alphabetical pattern is a series of alphabet (A-Z) that form a pattern or any shape like a pyramid, triangle, square, rhombus, etc. The alphabet pattern programs are very important for new developers. It improves thinking power and logical building ability in computer programming.
A pattern program consists of patterns of numbers, letters, and symbols in a particular form. In the programming language, the loop condition is considered important for printing patterns. Generally, nested loops may be used in alphabet patterns and shape printing programs.
The alphabet pattern programs need an ASCII code to print a letter. Like, the chr(65) function will return the letter 'A', the chr(66) function will return the letter 'B', and so on. Here, we have used nested loop conditions in the alphabet pattern and shape-building programs.
Alphabet Pattern Program 1
Suppose someone asks you to print the following pattern using the Python for loop.
A
AB
ABC
ABCD
ABCDE
The solution is as follows. In this alphabetic pattern program, we have used two nested for loops. As we know, 65 is the ASCII code of the letter 'A' and 69 is the ASCII code of the letter 'E'. The outer for loop works in the range(65,70) to print only 5 lines of alphabet. The inner for loop is responsible for printing the alphabets in each row. In the first iteration, it will print the value of chr(j) where j is 65, thus the letter 'A' is printed, and similarly, it will print the whole pattern.
# outer loop for ith rows
for i in range (65,70):
# inner loop for jth columns
for j in range(65,i+1):
print(chr(j),end="")
print()
Alphabet Pattern Program 2
Suppose we have to print repeated alphabet patterns of up to five rows, as shown below.
A
BB
CCC
DDDD
EEEEE
The Python programming solution to the above pattern is as follows. This alphabetic pattern program uses nested for loops. The inner for loop prints chr(i). As a result, only 'A' is printed in the first row, and chr(i) prints 'B' twice in the second row, and so on.
# outer loop for ith rows
for i in range (65,70):
# inner loop for jth columns
for j in range(65,i+1):
print(chr(i),end="")
print()
Alphabet pattern program 3
The alphabet pattern program of the given pattern is-
A
BC
DEF
GHIJ
KLMNO
PQRSTU
asciichr = 65
# outer loop for ith rows
for i in range(0,6):
# inner loop for jth columns
for j in range(0,i+1):
char = chr(asciichr)
print(char,end="")
asciichr += 1
print()
Alphabet pattern program 4
The alphabet pyramid pattern program is -
A
A B C
A B C D E
A B C D E F G
A B C D E F G H I
rows=5
# outer loop for ith row
for i in range (1,rows+1):
asciichr = 65
for space in range(rows-i):
print(' ',end=' ')
# inner loop for jth columns
for j in range(1, 2*i):
char = chr(asciichr)
print(char+' ' ,end="")
asciichr += 1
print()
Alphabet pattern program 5
The alphabet pattern program in shape of right reverse triangle -
A B C D E
F G H I
J K L
M N
O
rows = 5
num = rows
asciichr = 65
# outer loop for ith rows
for i in range(rows, 0, -1):
# inner loop for jth columns
for j in range(0, i):
char = chr(asciichr)
print(char+' ' ,end="")
asciichr += 1
print("\r")
Alphabet pattern program 6
Suppose, someone asks you to print an alphabet pattern program in the reverse shape of 'M' as shown below.
A A A A A A A A A A
B B B B B B B B
C C C C C C
D D D D
E E
The code will be as follows -
rows = 6
asciichr = 65
for i in range(0, rows):
for j in range(rows - 1, i, -1):
char = chr(asciichr)
print(char+' ' ,end="")
for l in range(i):
print(' ', end='')
for k in range(i + 1, rows):
print(char, '', end='')
print('\n')
asciichr += 1
Alphabet pattern program 7
Suppose someone asks you to print a diamond alphabet pattern made up of 'A' up to a specified number of lines, like this-
B
B B B
B B B B B
B B B B B B B
B B B B B B B B B
B B B B B B B
B B B B B
B B B
B
The code will be as follows-
# Upper triangle shape
upper =5
# outer loop for ith row
for i in range (1,upper+1):
for space in range(upper-i):
print(' ',end=' ')
# inner loop for jth columns
for j in range(1, 2*i):
print('B ',end="")
print()
# Lower triangle shape
lower = 4
for i in range (lower,0,-1):
print(' ',end=' ')
for space in range(lower-i):
print(' ',end=' ')
# inner loop for jth columns
for j in range(1, 2*i):
print('B ',end="")
print()
Alphabet pattern program 8
Suppose someone asks you to print an alphabet pattern program as shown below.
ABCDE
ABCD
ABC
AB
A
The code will be as follows-
# outer loop for ith rows
for i in range (70,65,-1):
# inner loop for jth columns
for j in range(65,i):
print(chr(j),end="")
print()
Alphabet pattern program 9
Suppose someone asks you to print an alphabet pattern program as shown below.
AAAAA
BBBBB
CCCCC
DDDDD
EEEEE
The code will be as follows-
# outer loop for ith rows
for i in range (65,70):
# inner loop for jth columns
for j in range(65,70):
print(chr(i),end="")
print()
Alphabet pattern program 10
Suppose someone asks you to print an alphabet pattern program as shown below.
P
PQ
PQR
PQRS
PQRST
PQRSTU
The code will be as follows-
# outer loop for ith rows
for i in range (ord('P'),ord('U')+1):
# inner loop for jth columns
for j in range(ord('P'),i+1):
print(chr(j),end="")
print()
Alphabet pattern program 11
Suppose someone asks you to print an alphabet pattern program as shown below.
ABCDE
ABCD
ABC
AB
A
The code will be as follows-
# outer loop for ith rows
for i in range (ord('P'),ord('U')+1):
# inner loop for jth columns
for j in range(ord('P'),i+1):
print(chr(j),end="")
print()
Alphabet pattern program 12
Suppose someone asks you to print an alphabet pattern program in a triangle shape, as shown below.
o
o o o
o o o o o
o o o o o o o
o o o o o o o o o
o o o o o o o o o o o
o o o o o o o o o o o o o
The code will be as follows-
rows=7
# outer loop for ith rows
for i in range (1,rows+1):
for space in range(rows-i):
print(' ',end=' ')
# inner loop for jth columns
for j in range(1, 2*i):
print('o ',end="")
print()
Alphabet pattern program 13
Suppose someone asks you to print an alphabet square pattern program as shown below.
A B C D E
F G H I J
K L M N O
P Q R S T
U V W X Y
The code will be as follows-
# alphabetic square alphabet pattern
size = 5
count = 0
for i in range(size):
for j in range(size):
print(chr(65 + count), end=" ")
count += 1
print()
Alphabet pattern program 14
Suppose someone asks you to print an alphabet diamond pattern program as shown below.
A
ABC
ABCDE
ABC
A
The code will be as follows-
# diamond alphabet pattern program
n = 3
# Upper triangle shape
for i in range(n):
for j in range(n - i - 1):
print(' ', end='')
for j in range(2 * i + 1):
print(chr(65 + j), end='')
print()
# Lower triangle shape
for i in range(n - 1):
for j in range(i + 1):
print(' ', end='')
for j in range(2*(n - i - 1) - 1):
print(chr(65 + j), end='')
print()
Alphabet pattern program 15
Suppose someone asks you to print the specific string 'PRISKA' as a pattern.
P
PR
PRI
PRIS
PRISK
PRISKA
The code will be as follows -
str= "PRISKA"
# Outer loop
for i in range(0,7):
# inner loop
for j in range(0,i+1):
print(str[j],end="")
print()
Alphabet pattern program 16
Suppose someone asks you to print the pattern in decreasing order like this-
A
BA
CBA
DCBA
EDCBA
FEDCBA
The code will be as follows-
# Outer loop
for i in range(65,71):
# Inner loop
for j in range(i,64,-1):
print(chr(j),end="")
print()
Alphabet pattern program 17
Suppose someone asks you to print an alphabet pattern program in reverse triangle shape as shown below.
D D D D D D D D D
D D D D D D D
D D D D D
D D D
D
The code will be as follows-
rows=5
# outer loop for ith rows
for i in range (rows,0,-1):
for space in range(rows-i):
print(' ',end=' ')
# inner loop for jth columns
for j in range(1, 2*i):
print('D ',end="")
print()
Alphabet pattern program 18
Suppose someone asks you to print a hollow triangle alphabet pattern as shown below.
A
AB
A B
A B
A B
A B
ABCDEFG
The code will be as follows-
row = 7
for i in range(1, row+1):
count = 0
for j in range(i):
if j == 0 or j == i-1:
print(chr(65 + count), end='')
count += 1
else:
if i != row:
print(' ', end='')
else:
print(chr(65 + count), end='')
count += 1
print()
Alphabet pattern program 19
Suppose someone asks you to print hourglass pattern as shown below.
ABCDEFGHI
ABCDEFG
ABCDE
ABC
A
ABC
ABCDE
ABCDEFG
ABCDEFGHI
The code will be as follows-
# hourglass alphabet pattern
row = 5
# Lower triangle shape
for i in range(row-1):
for j in range(i):
print(' ', end='')
for k in range(2*(row-i)-1):
print(chr(65 + k), end='')
print()
# Upper triangle shape
for i in range(row):
for j in range(row-i-1):
print(' ', end='')
for k in range(2*i+1):
print(chr(65 + k), end='')
print()
Related Articles
Remove element from list PythonPascal triangle pattern in Python
Hollow Diamond Pattern in Python
Reverse pyramid pattern in Python
Vader Sentiment Analysis Python
isalpha Python
Python YouTube Downloader Script
Python project ideas for beginners
Pandas string to datetime
Fillna Pandas Example
Lemmatization nltk
How to generate QR Code in Python using PyQRCode
OpenCV and OCR Python
PHP code to send SMS to mobile from website
Fibonacci Series Program in Python
Python File Handler - Create, Read, Write, Access, Lock File
Python convert XML to JSON
Python convert xml to dict
Python convert dict to xml