Python Program To Print a to z using for loop and chr function

By | March 29, 2020

Topic: Python Program To Print a to z using for loop and chr() function

We will use the built-in Python function chr() to print the small alphabets from a to z. We know that the Ascii code of ‘a’ is 97 and that of ‘z’ is 122.

Python program to print a to z using a for loop

Therefore we will use a range() function in a for loop with arguments 97, 123. 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(97,123):
		print(chr(i), end=" ")


main()

The output 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(97,123) function?

The output of the range(97,123) function is the sequence from 97 through 122. We use this range() function with the list of lower case alphabets in our mind.

We note that 97 is the ascii code of small letter ‘a’ as well as ‘z’ has the ascii code of 122.

Similarly we can use a function chr(97) to print character represented by the ascii code 97.

What is the output of chr(97)?

chr(97) will return the small letter ‘a’.

chr(98) will give a ‘b’, chr(99) will return a ‘c’ and so on. Hence the last letter ‘z’ is the output from chr(122). The for loop will use all 26 numbers from 97 to 122, each will be used to print a corresponding lower case alphabet.

Therefore, we get a list of alphabets in lower case using the above mentioned for loop, which is the required output in this program.

You will also like the following program in Python

Python Program to Print A to Z using for Loop

Leave a Reply

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