Python program to print alphabetical pattern
In this tutorial, you are going to learn how to print various alphabetic pattern in Python. Alphabetical (A-Z) pattern is a series of alphabet which forms a pattern or any shape like triangle, square, rhombus etc. These patterns are created with the help of nested for loop. To create the alphabetic pattern, you should know that how to convert the ASCII value into the ASCII character.
chr(): This function is used to convert the ASCII value into the ASCII character.
ASCII characters ASCII value
A-Z [65-90]
a-z [97-122]
0-9 [48-57]
Special symbols [0-47,58-64,91-96,123-127]
Print Alphabetic Pattern 1: Python
- During the first iteration of the outer loop, the i becomes 65 and it goes into the inner loop
- The inner loop will work for the range(65,i+1) {i.e., (65,66), (65,67), (65,68), (65,69), (65, 70) for five consecutive iterations of the outer loop}
- The inner loop will print the ASCII character of j by converting the ASCII value.
- Print() takes the pointer in the next line.
- The outer loop will continue until i become 69 till repeat all the above steps to print the pattern.
# outer loop for i in range (65,70): # inner loop for j in range(65,i+1): print(chr(j),end="") print()
Output:-
A AB ABC ABCD ABCDE
Print Alphabetic pattern 2: Python
This pattern is similar to the above pattern in working. The only difference is that, the inner loop is printing the ASCII character of i instead of j.
# outer loop for i in range (65,70): # inner loop for j in range(65,i+1): print(chr(i),end="") print()
Output:-
A BB CCC DDDD EEEEE
Alphabetical pattern 3:
This pattern is similar to the above pattern. The only difference is that, we are storing the value of i in a variable and printing that variable again and again by incrementing it side by side to form the pattern.
# Outer loop for i in range(65,70): k=i # Inner loop for j in range(65,i+1): print(chr(k),end="") k=k+1 print()
Output:-
A BC CDE DEFG EFGHI
Alphabetic pattern 4:
This pattern is used when you have to print the specific string as a pattern.
- The specific string is input in the str whose pattern is to be created.
- Set the range of the outer and inner loop according to the string length.
- Print the str character as the pattern with the help of indexing using j.
- Print() will takes the pointer in the next line. The first iteration is complete.
- The outer loop will continue until i become 6 till repeat all the above steps to print the pattern.
str= "APOORVA" # string # Outer loop for i in range(0,7): # inner loop for j in range(0,i+1): print(str[j],end="") print()
Output:-
A AP APO APOO APOOR APOORV APOORVA
Alphabetical pattern 5:
- During the first iteration of the outer loop, then i has the value 65 and goes into the inner loop.
- The inner loop will work for the range (i,64,-1) which means that it will work in the decrement order and print the pattern like this DCBA.
- Print the j using chr() function to get the ASCII character.
- Print() will takes the pointer in the next line. The first iteration is complete.
- The outer loop will continue until i become 69 till repeat all the above steps to print the pattern.
# Outer loop for i in range(65,70): # Inner loop for j in range(i,64,-1): print(chr(j),end="") print()
Output:-
A BA CBA DCBA EDCBA
Alphabetic pattern 6:
This pattern is called pyramid pattern. It has three inner loops.
- Set a variable according to the white space of the left side of the pyramid.
- The outer loop will work for the range (65,70).
- Then it goes to the inner loop 1. This loop will print the white space. The white space will decrement line by line as the for loop range is set in negative.
- The inner loop 2 will print the alphabetic pattern on the left side.
A AB ABC ABCD ABCDE
- The inner loop 3 will print the pattern on the right side with the first line blank and other line has the alphabetic pattern.
A AB ABC ABCD
- By combining all the inner loops the pattern will be formed.
m=6 # Outer loop for i in range(65,70): m=m-1 # Inner loop 1 for j in range(m,1,-1): print(" ",end="") # Inner loop 2 for k in range(65,i+1): print(chr(k),end="") # Inner loop 3 for n in range(65,i): print(chr(n),end="") print()
Output:-
A ABA ABCAB ABCDABC ABCDEABCD
Alphabetical pattern 6:
- During the first iteration of the outer loop, then i has the value 65 and goes into the inner loop.
- The inner loop will work for the range (i,64,-1) which means that it will work in the decrement order and print the pattern like this DCBA.
- Print the j using chr() function to get the ASCII character.
- Print() will takes the pointer in the next line. The first iteration is complete.
- The outer loop will continue until i become 69 untill repeat all the above steps to print the pattern.
# Outer loop for i in range(65,70): # Inner loop for j in range(i,64,-1): print(chr(j),end="") print()
Output:-
A BA CBA DCBA EDCBA
Star(asterisk) pattern in Python
pls tell me how to generate alpha pattern using any other option, not ascii values
You can generate the pattern without using Ascii values with the help of below code:
from string import ascii_uppercase
for i in range(1,7):
print(“”. join(ascii_uppercase[:i]))
The ascii_uppercase function from string package will convert the value of i into the capital alphabets. The ascii_uppercase contains the alphabets from [A-Z].
You, can also use the similar functions like
ascii_lowercase that contains alphabets from [a-z].
Or ascii_letters function that contains alphabets from [a-zA-Z].
Hey, How do you print this pattern?
APQR
ABQR
ABCR
ABCD
Thanks!
for i in range(4):
for j in range(4):
if(i<j):
print(chr(65+14+j), end=" ")
else :
print(chr(65+j), end=" ")
print()
plz print this pattern
A
BAB
CBABC
DCBABCD
EDCBABCDE
You can use this code to produce the above pattern:
m=5
for i in range (65,70):
m=m-1
for j in range(m, 0,-1):
print(” “, end=””)
for k in range(i, 65, -1):
print(chr(k),end=””)
for n in range(65,i+1):
print(chr(n), end=””)
print()
1
1 D
1 D O
1 D O D
1 D O D O
can anyone help me in printing this pattern?
l=[1,’D’,’O’,’D’,’O’]
for i in range(0,5):
for j in l[0:i+1]:
print (j,end=” “)
print()
Output:-
1
1 D
1 D O
1 D O D
1 D O D O
Help me to print this pattern
a b c d
e f g
h i
j
a=97
for i in range(4,0,-1):
for j in range(97,97+i):
print(chr(a),end=” “)
a=a+1
print()
Output:-
a b c d
e f g
h i
j
Input=2
B B B
B A B
B B B
program for this?
How do I encode
A
BC
DEF
GHIJ
GHIJ
DEF
BC
A in python
——–e——–
——e-d-e——
—-e-d-c-d-e—-
–e-d-c-b-c-d-e–
e-d-c-b-a-b-c-d-e
–e-d-c-b-c-d-e–
—-e-d-c-d-e—-
——e-d-e——
——–e——–
If the output is:
a
Bc
DeF
gHiJ
kLmNo
Then the program is ?