Alphabet Pattern Programs in Python

(Last Updated On: 08/09/2023)

Alphabet pattern programs are very important in logical building programs for Python developers. These projects improve the thinking power of the learner developers to tackle a given issue. 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. In this article, you will learn Alphabet pattern programs using Python.

Alphabet pattern programs need an ASCII code to print letters. Like, chr(65) function will return the letter ‘A’, chr(66) function will return letter ‘B’ and so on. Here, we have used nested loop conditions in alphabet patterns and shape-building programs.

In Python, a for loop is used to print the various patterns. 

  • The outer loop prints the number of rows.
  • The inner loops print the number of columns.
  • The variable is to print white space according to the required place in Python.

For all the practice Videos and Explanations on Python, please click over here. Python Practice Series.

Video Explanation of Alphabet pattern in Python

Program to Print Alphabet pattern in Python

Method 1: Alphabet Pyramid Pattern I

Source code:

for i in range (65,70):
    for j in range(65,i+1):
        print(chr(j),end="")
    print()
Output:
A
AB
ABC
ABCD
ABCDE

Code Explanation Method 1: Alphabet Pyramid Pattern I

The above program is pretty straightforward. We have used the chr function to print the alphabet. 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]

In this for loop, we have i as the counter of the loop, and this loop will run from 65 to 70. Inside of the for loop, we have another for loop, the outer loop is worked for rows of the pyramid, and this loop is responsible for printing alphabets. During the first iteration of the outer loop, i becomes 65, and it goes into the inner loop.

The inner loop will work for the range(65,i+1). i.e., range(65,66), range(65,67), range(65,68), range(65,69), range(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. And we have used end statements to keep on printing on the same line.

Print() takes the pointer in the next line. The outer loop will continue until I become 69 till I repeat all the above steps to print the pattern.

Method 2: Alphabet Pyramid Pattern II

Source Code:

for i in range (65,70):
    for j in range(65,i+1):
        print(chr(i),end="")
    print()
Output:
A
BB
CCC
DDDD
EEEEE

Code Explanation Method 2: Alphabet Pyramid Pattern II

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. In this for loop, we have i as the counter of the loop, and this loop will run from 65 to 70. This loop is responsible for printing rows. Inside for loop, we have another for loop which will print alphabets with the help of the chr function of python.

So the inner loop has j as a counter, and this loop will also start from 65 to i+1, because if we have defined a range from 1 to 5 then the loop will run from only 4 times as the range function opt-out the last number.

If you see, we want to print the same alphabet on a single line like A on first, on second line B for 2 times, and so on. To handle this, we will make a change in a print statement instead of j we will print i, so for the first iteration program will print A. The second program will print BB and so on.

Method 3: Alphabet Pyramid Pattern III

Source Code:

str = input(“Enter any name:”)
for i in range(0 , len(str)):
    for j in range(0,i+1):
        print( str[j] , end="")
    print() 
Output:
Enter any name:Newtum
N
Ne
New
Newt
Newtu
Newtum

Code Explanation Method 3: Alphabet Pyramid Pattern III

In this program, we will take user input and store it into an str variable. In the next line, we have a for loop which is responsible for printing rows, and it will start from 0 and run till the length of the str, for we have used the len() function of the python. Inside for loop, we have another for loop which will print the single letter on a single line, and keep on increasing as per the new row.

This for loop will run from 0 to i+1, and inside for loop, we have a print statement that will print the jth index of str. In the case of the first iteration, the inner loop will run from 0 to 1 hence N will be printed. Then at the time of the second iteration loop will run from 0 to 2, so Ne will print and so on.

Method 4: Alphabet Pyramid Pattern IV

Source Code:

# 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

Code Explanation Method 4: Alphabet Pyramid Pattern IV

Let’s understand how this program works; as you see, we have two for loops in every program. One is used for printing rows, and another is used for printing columns or the alphabet. The outer loop will run 5 times as its range is specified from 65 to 70. This means the loop will run till i is less than 70.

Inside of this for loop, we have another for loop which has j as counter and in range; if you see, we have 3 parameters first is initialization, second is condition, and 3rd is steps or increment or decrement. In all the above programs, we have an incrementing loop, but in this, we have used a decrementing loop just because we have to print reverse alphabets.

So this loop will start from the value of i it will run till j value is greater than 64, and the print statement is used to print the string. During the first iteration of the outer loop, then i have the value 65 and go 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 take the pointer in the next line. The first iteration is complete. The outer loop will continue until I become 69 till I repeat all the above steps to print the pattern.

About The Author