Site icon codippa

Print lower case letters in python in 2 ways

This article will explain two different ways to write a python program to print all the letters or alphabets from a to z in lower case.
Method 1: Using string module
Python string module has a variable named ascii_lowecase which is a string of all alphabets from a to z in lower case.
Iterating over this string using a for-in loop will return a letter one by one. Example,

import string

for char in string.ascii_lowercase:
     print(char, end=' ')

Output is

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

Method 2: Using chr() function
ASCII values of lower case letters range from 97 to 123 where 97 is the value for ā€˜aā€™ and 123 is for ā€˜zā€™.
This integer when supplied to chr() function, it returns the string of one character corresponding to the integer.

Thus, if we iterate from 97 to 123 using a for loop, we can get the letters from a to z as shown below.

for char in range(97, 123):
     print(chr(char), end=' ')

If you need to write a program without using any python module, then this is the right answer.
Output is

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

Hit the clap below if this was useful.
Exit mobile version