Python program to print alphabetical pattern – All
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 7:
- 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
Some more alphabetical patterns
Program 1:
n=int(input("Enter the number of rows: ")) for i in range(1,n+1): print(" "*(n-i),(str(chr(64+i)+" "))*(2*i-1))
Output:
Enter the number of rows: 5 A B B B C C C C C D D D D D D D E E E E E E E E E Program 2:
num=int(input("Enter a number:")) for i in range(1,num+1): for j in range(1,i+1): print(chr(65+num-j),end=" ") print() for a in range(1,num+1): for k in range(num-a,0,-1): print(chr(64+k+a),end=" ") print()
Output:
Enter a number:10 J J I J I H J I H G J I H G F J I H G F E J I H G F E D J I H G F E D C J I H G F E D C B J I H G F E D C B A J I H G F E D C B J I H G F E D C J I H G F E D J I H G F E J I H G F J I H G J I H J I J
Program 3:
num=int(input("Enter a number:")) for i in range(1,num+1): print(" "*(i-1),end="") for j in range(1,num+2-i): print(chr(65+num+1-i-j),end=" ") print()
Output:
Enter a number:10 J I H G F E D C B A I H G F E D C B A H G F E D C B A G F E D C B A F E D C B A E D C B A D C B A C B A B A A
program 4:
num=int(input("Enter a number:")) for i in range(1,num+1): print(" "*(num-i),end="") for j in range(1,i+1): print(chr(64+j),end=" ") print() for p in range(1,num): print(" "*p,end="") for q in range(1,num+1-p): print(chr(64+q+p),end=" ") print()
Output:
Enter a number:8 A A B A B C A B C D A B C D E A B C D E F A B C D E F G A B C D E F G H B C D E F G H C D E F G H D E F G H E F G H F G H G H H
Someone asked in a comment to print this pattern:
A B C D E F G H I F G D E B C A
Here is the program:
# A given value of N will produce (2 * N - 1) rows, of which the first N rows # will be unique and the remaining (N - 1) rows will be mirrored num_rows = 5 # The code of the starting character start = 65 # Produce N unique rows: first row has one character, all others have two rows = [chr(start)] + [f"{chr(start + 2 * i + 1)} {chr(start + 2 * i + 2)}" for i in range(num_rows - 1)] if num_rows > 0 else [] # Produce (N - 1) mirrored rows by reversing everything but the last row, # join with line breaks, then print print("\n".join(rows + rows[-2::-1]))
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].
How to print this pattern
ABCDE
1234
ABC
12
A
n=5
st=n
for i in range(n):
print(‘ ‘*(n-1-i),end=’ ‘)
for j in range (st):
if i%2==0:
print(chr(65+j),end=’ ‘)
else:
print(j+1,end=’ ‘)
st-=1
print()
user=int(input(“Enter the string: “))
ascii_value=65
for i in range(user):
for j in range(user-i):
print(chr(ascii_value+j),end=” “)
print()
for k in range(user-i):
print(k+1,end=” “)
print()
size = 5
for i in range(size):
for j in range(1, size – i):
print(” “, end=””)
for k in range(i + 1):
print(chr(65 + k), end=””)
print()
A
C B
D E F
I H G
J K L M
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()
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
for i in range(65,70):
for j in range(i,64,-1):
print(chr(j),end=” “)
for n in range(66,i+1):
print(chr(n),end=” “)
print()
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()
U can use this code also
n=5
For u in range(n):
p=65
for j in range(i+1):
print(chr(p+i), end=(” “))
p=66
for k in range(i) :
print(chr(p), end=(” “))
p+=1
print()
How to generate this pattern ?
——–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——–
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?
Below Code will give you the required output
x = int(input(“Enter Number Between 2 and 26 : “))
d = {}
ch = 65
for i in range(1,27):
d[i] = chr(ch)
ch+=1
for i in range(1,4):
for j in range(1,4):
if i == 2 and j == 2:
print(d[x-1],end=””)
else:
print(d[x],end=””)
print()
for i in range(3):
for j in range(3):
if (i & j)==1:
print(chr(65),end=””)
else:
print(chr(65+1),end=””)
print ()
Output:-
BBB
BAB
BBB
How do I encode
A
BC
DEF
GHIJ
GHIJ
DEF
BC
A in python
Below code will print above pattern:
for i in range(0,4):
for j in range(0,i+1):
print(chr(k),end=””)
k+=1
print()
for i in range(4,0,-1):
for j in range(i):
print(chr(l),end=””)
l+=1
print()
l-=t
t-=2
——–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——–
n=int(input())
if n==0:
print()
elif n==1:
print(‘a’)
else:
s=’abcdefghijklmnopqrstuvwxyz’
a=s[0:n][::-1]
l=[]
for i in range(n):
b=a[0:i+1]
c=list(b)
c=’-‘.join(c)
l.append(c)
x=s[1:n][::-1]
l1=[]
for i in range(n):
d=x[0:i][::-1]
d=list(d)
t=’-‘.join(d)
t=’-‘+t
l1.append(t)
for i in range(n):
print(l[i].rjust((n*2)-1,’-‘),end=”)
print(l1[i].ljust((n * 2) – 2, ‘-‘))
l.reverse()
l.remove(l[0])
l1.reverse()
l1.remove(l1[0])
for i in range(n-1):
print(l[i].rjust((n*2)-1,’-‘),end=”)
print(l1[i].ljust((n * 2) – 2, ‘-‘))
If the output is:
a
Bc
DeF
gHiJ
kLmNo
Then the program is ?
a=65
for i in range(1,6):
for j in range(0,i):
print(chr(a),end=” “)
a=a+1
print()
Arnab be like – kuch bhii…
Today is my cs exam, so wanted to see patterns, very nice content.
Write a python program to print the following pattern:
A B C D
E F G
H I
J
Pls help me with this program
A
B C
D E F
G H I J
# Python3 code for triangular
# patterns of alphabets
if __name__ == ‘__main__’:
n = 5;
for i in range(1, n + 1):
for j in range(1, i + 1):
print(chr(ord(‘A’) + j – 1),
end = ” “);
print(“”);
# This code is contributed by 29AjayKumar
pls help me for this pattern
A
B B
C C
D D
E E
D D
C C
B B
A
Maam please help me in this pattern program.
P
P Y
P Y T
P Y T H
P Y T H O
P Y T H O N
str=[‘p’,’y’,’t’,’h’,’o’,’n’]
for i in range(7):
for j in range(0,i+1):
print(str[j],end=” “)
print()
val = “PYTHON”
for i in range ( len ( val) ):
print( val [: i+1])
print()
string=”PYTHON”
i=0
for x in range(len(string)+1):
print(string[i:x])
I know how to write a code for this pattern using for loop
string=”PYTHON”
k=0
for x in range(len(string)+1):
print(string[k:x])
str=[‘p’,’y’,’t’,’h’,’o’,’n’]
for i in range(6):
for j in range(0,i+1):
print(str[j],end=” “)
print()
how to reverse the alphabetic pattern no. 6
A
ABA
ABCAB
ABCDABC
ABCDEABCD
in reverse piramid
n = 6
String(‘A’, ‘B’, ‘C’)
Output:
A
A B
ABC
ABCA
ABCAB
ABCABC
What is the code for this
n = 6
String = [‘A’, ‘B’, ‘C’]
OUTPUT:
A
AB
ABC
ABCA
ABCAB
ABCABC
For pattern like
A
B B
C C
D D
E E
D D
C C
B B
A
mam plz help me to print this pattern
A
B
C
D E F
G H I
J
K L M N
O
How do you print this –
A
B B
C C C
D D D D
E E E E E
plzz print this pattern
A
AB
ABC
ABCD
ABCDE
using def function
What is the code for this
n = 6
String = [‘A’, ‘B’, ‘C’]
OUTPUT:
A
AB
ABC
ABCA
ABCAB
ABCABC
how to print this
1
12A
123BA
1234CBA
for 10 rows like this
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 z
How to get this pattern
a=96
for i in range(0,7):
for j in range(0,4):
a=a+1
if (a==123)or (a==124):
continue
print (chr(a),end=” “)
print(”)
Plzz print this
d
ade
gadee
agadees
jagadeesh
Do help in printing this pattern
A
B C
D E F
G H I J
K L M N O
P Q R S T U
a= 65
for i in range(0, 7):
for j in range(0, i + 1):
char= chr(a)
print(char, end=’ ‘)
alfa+= 1
print(” “)
for i in range(0,6):
for j in range(0,i+1):
print(chr(a),end=” “)
a = a+1
print()
A
A&B
A&B&C
A&B&C&D
A&B&C&D&E
python code for this pattern
Help to print
A
A&B
A&B&C
A&B&C&D
P
R O
G R A
M P R 0
G R A M P
R 0 G R A M
P R 0 G R A M
please print this in python
H H
H H H
H H
Right down mirror alpha Pattern:
ABCDE
ABCD
ABC
AB
A
A B C D E F
A B C D E
A B C D
A B C
A B
A
alphabets=’a,b,c,d,e,f’
string_list = alphabets.split(‘,’)
for i in range(0,6):
print(string_list[i:])
K
N K E
I N K E S
R I N K E S H
Plz solve it !!
Please teach me how to do this code:
ABCD
DEFG
GHIJ
JKLM
x=65
for i in range (4):
for j in range (x, x+4):
print (chr(j), end=””)
print ()
x+=3
A B C D E
P Q R S F
O X Y T G
N W V U H
M L K J I
A1B1C1
A2B2C2
A3B3C3
A4B4C4
A5B5C5
PRINT IN PYTHON WITH LOOP FOR
a=’a,b,c,d,e’
s=a.split(‘,’)
for i in range(0,6):
print(s[i:])
ANSWER:——-
[‘a’, ‘b’, ‘c’, ‘d’, ‘e’]
[‘b’, ‘c’, ‘d’, ‘e’]
[‘c’, ‘d’, ‘e’]
[‘d’, ‘e’]
[‘e’]
[]
Hey, how do we print this
A
B C D
E F G H
From number 3 question to print:
APQR
ABQR
ABCR
ABCD
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()
how to get this program in python using a for loop i cant get it to be A one the first line and then BC on the second and so fourth i keep getting A, AB, ABC instead
A
BC
DEF
GHIJ
python3?
How to print the pattern
W
XW
YYX
ZZZY
I Need A Program To Print Text In Rows And Cols Based On Occurance Of Character
This Is The Give Word “pppppeeeeaabbbxxxxxk”
The Expcted OutPut Is
k
aa
bbb
eeee
ppppp
xxxxxx
Write a program to print the below Pyramid
Input: 4
W
XW
YYX
ZZZY
Input: 5
V
WV
XXW
YYYX
ZZZZY
A
B C
D E
F G
H I
F G
D E
B C
A
Try to write a code to print this pattern using python
I have updated the blog and solve your alphabetical pattern. Check it.
A
B C
D E
B C
A
Can u pls write a code to print this pattern using python
A
B C
D E
F G
H I J K L M N O P
how do you print this pattern?
—-A
—B-C
–D–E
-F—G
HIJKLMNO
how do you print this?