Python Program To Display Characters From A to Z

Problem Definition

Create a Python program to display all alphabets from A to Z.

Solution

This article will go through two pythonic ways to generate alphabets.

Using String module

Python’s built-in string module comes with a number of useful string functions one of them is string.ascii_lowercase

Program

import string

for i in string.ascii_lowercase:
    print(i, end=" ")

Output

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

The string.ascii_lowercase method returns all lowercase alphabets as a single string abcdefghijklmnopqrstuvwxyzso the program is simply running a for loop over the string characters and printing them.

Similarly for uppercase A to Z letters.

Program

import string

for i in string.ascii_uppercase:
    print(i, end=" ")

Output

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

Using chr() Function

The chr() function in Python returns a Unicode character for the provided ASCII value, hence chr(97) returns “a”.

To learn more about chr() read – Python Program To Get ASCII Value Of A Character

Program

for i in range(97,123):
    print(chr(i), end=" ")

Output

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

The ASCII value for a is 97 and for z is 122. Therefore, looping over every integer between the range returns the alphabets from a-z.

ASCII value for capital A is 65 and for capital Z 90.

Program

for i in range(65,91):
    print(chr(i), end=" ")

Output

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

Leave a Comment