Topic: Python Program to Print A to Z using for Loop
We will use the built-in Python function chr() to print the A to Z alphabet characters. We know that the Ascii code of ‘A’ is 65 and that of ‘Z’ is 90.
Therefore we will use a range() function in a for loop with arguments 65,91. This for loop will print A to Z alphabets.
# Python program to print # A to Z using for loop def main(): for i in range(65,91): print(chr(i), end=" ") main()
The output of this Python program will be as follows:
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 This Program will work?
Actually, the for loop is the main statement in this program to print A to Z alphabets using a loop.
What is the output of range(65,91) function?
The output of the range(65,91) function is the sequence from 65 through 90. We use this range() function with the list of upper case alphabets in our mind.
What is the ascii code of ‘A’?
We note that 65 is the ascii code of capital letter ‘A’ as well as ‘Z’ has the ascii code of 90.
Similarly we can use a function chr(65) to print character represented by the ascii code 65.
What is the output of chr(65)?
chr(65) will return the small letter ‘A’.
chr(66) will give a ‘B’, chr(67) will return a ‘C’ and so on. Hence the last letter ‘Z’ is the output from chr(90). The for loop will use all 26 numbers from 65 to 90, each will be used to print a corresponding upper case alphabet.
Therefore, we get a list of alphabets in upper case using the above mentioned for loop, which is the required output in this program.
You will also like to read the Python Program:
Print ‘a’ to ‘z’ lower case alphabets using a for loop