Python Programs To Print Alphabet Pattern

Alphabet pattern programs are very important in logical building programs for the Python developers. These projects improve thinking power about the learner developers to tackle a given issue.

A pattern program consists of patterns of numbers, letters, 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 to print the number of rows.
  • The inner loops to print the number of columns.
  • The variable to print white space according to the required place in Python.

We want you to know about the Alphabet Pattern in Python, hence we are come up with the best video explanation you have ever watched on the internet, where you not only understand the logic but also code to get the desired output.

You still face difficulties in understanding the logic? That’s perfectly fine, we have something more for you. You can scroll down to learn more about this topic.

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

Video Explanation to Alphabet pattern in Python

Code: Alphabet pattern in Python

Method 1: Alphabet Pyramid Pattern I

Expected Output:

A
AB
ABC
ABCD
ABCDE

Source code:

for i in range (65,70):
    for j in range(65,i+1):
        print(chr(j),end="")
    print()

Code Explanation Method 1: Alphabet Pyramid Pattern I

Above program is pretty straight forward. 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

A-Z

a-z

0-9

Special symbols 

ASCII Value

[65-90]

[97-122]

 [48-57]

   [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

Expected Output:

A
BB
CCC
DDDD
EEEEE

Source Code:

for i in range (65,70):
    for j in range(65,i+1):
        print(chr(i),end="")
    print()

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 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

Expected Output:

N
Ne
New
Newt
Newtu
Newtum 

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 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 that 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, 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.

I hope now the alphabet pattern is clear to you. 

Method 4: Alphabet Pyramid Pattern IV

Expected Output:

A
BA
CBA
DCBA
EDCBA

Source Code:

# Outer loop
for i in range(65,70):
    # Inner loop
    for j in range(i,64,-1):
        print(chr(j),end="")
    print()

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 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 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.

Facebook Comments
(Visited 1 times, 1 visits today)

Leave A Comment

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