2 min read
Create a Python program to display all alphabets from A to Z.
This article will go through two pythonic ways to generate alphabets.
Python's built-in string module comes with a number of useful string functions one of them is string.ascii_lowercase
import string
for i in string.ascii_lowercase:
print(i, end=" ")
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 abcdefghijklmnopqrstuvwxyz
so the program is simply running a for loop over the string characters and printing them.
Similarly for uppercase A to Z letters.
import string
for i in string.ascii_uppercase:
print(i, end=" ")
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 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
for i in range(97,123):
print(chr(i), end=" ")
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.
for i in range(65,91):
print(chr(i), end=" ")
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 from djangocentral
2 min read
2 min read