Given a task to print the letters from A to Z, you should write a Python logic where the Python program print all the letters between A and Z
There are two ways to solve this Python program and both the program are only of four lines.
Write A Python Program To Print A To Z
Let's take a character variable named as letter and set it to capital “A”. Using the print statement prints a user friendly message that is letters from A to Z is …. Take a for loop and straight from 0 to 26 and get the index. Using the print statement evaluate “ord(A) + index” Then convert the result to chr() and print the letter
letter = "A"
print("Letters from A - Z are ...")
for index in range(26):
print(chr(ord(letter) + index), end=" ")
Output
=============
Letters from A - Z are ...
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
Now if we want to print the lowercase letter a - z, Use the same logic as explained above, take a character variable and assign “a” to it. Get index from 0 to 26 and keep adding the index to ord(“a”) and then convert the result into a character using chr() function call
letter = "a"
print("Letters from a - z is ...")
for index in range(26):
print(chr(ord(letter) + index), end=" ")
Output
============
Letters from a - z 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
There is another way to print letters using the built-in string variable ascii_lowercase/ ascii_uppercase. Import the string module and get the ascii_lowercase Stored into a variable named as letter and using the following iterate all the letters and read it
from string import ascii_lowercase
letters = ascii_lowercase
print("Letters from a - z is ...")
for letter in letters:
print(letter, end=" ")
OUTPUT
===============
Letters from a - z 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
You can use the built-in type ascii_uppercase present in the string library just import it and print all the letters as shown below.
from string import ascii_uppercase
letters = ascii_uppercase
print("Letters from A - Z are ...")
for letter in letters:
print(letter, end=" ")
Output
===============
Letters from A - Z are ...
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
Conclusion
===============
These are the two ways to print the letters from A to Z, if you know any other method to print the letter comment down below.
0 Comments