Python Program To Display Characters From A to Z

2 min read

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

PROGRAMS

Latest Articles

Latest from djangocentral

Capturing Query Parameters of request.get in Django

In Django, the request object contains a variety of information about the current HTTP request, including the query parameters. Query parameters are a way to pass additional information in the URL and are used to filter or sort data. The request object p…
Read more →

2 min read

Understanding related_name in Django Models

In Django, related_name is an attribute that can be used to specify the name of the reverse relation from the related model back to the model that defines the relation. It is used to specify the name of the attribute that will be used to access the relat…
Read more →

2 min read