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

Django 4.1 adds async-compatible interface to QuerySet

The much-awaited pull request for an async-compatible interface to Queryset just got merged into the main branch of Django.Pull Request - https://github.com/django/django/pull/14843 The Django core team has been progressively adding async suppor…
Read more →

3 min read

Making Django Admin Jazzy With django-jazzmin

Django admin is undoubtedly one of the most useful apps of Django. Over the years there has been very little change in the admin app as far as the UX is concerned and it's not a bad thing at all. Django admin was designed to provide a simple and minimali…
Read more →

4 min read