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

  1. During the first iteration of the outer loop, the i becomes 65 and it goes into the inner loop
  2. 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}
  3. The inner loop will print the ASCII character of j by converting the ASCII value.
  4. Print() takes the pointer in the next line.
  5. 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.

  1. The specific string is input in the str whose pattern is to be created.
  2. Set the range of the outer and inner loop according to the string length.
  3.  Print the str character as the pattern with the help of indexing using j.
  4. Print() will takes the pointer in the next line. The first iteration is complete.
  5. 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:

  1. During the first iteration of the outer loop, then i has the value 65 and goes into the inner loop.
  2. 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.
  3. Print the j using chr() function to get the ASCII character.
  4.  Print() will takes the pointer in the next line. The first iteration is complete.
  5. 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.

  1. Set a variable according to the white space of the left side of the pyramid.
  2. The outer loop will work for the range (65,70).
  3. 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.
  4. The inner loop 2 will print the alphabetic pattern on the left side.
         A
        AB
       ABC
      ABCD
     ABCDE
  5. 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
  6. 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:

  1. During the first iteration of the outer loop, then i has the value 65 and goes into the inner loop.
  2. 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.
  3. Print the j using chr() function to get the ASCII character.
  4. Print() will takes the pointer in the next line. The first iteration is complete.
  5. 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


Leave a Reply

Your email address will not be published. Required fields are marked *