In this article, we will learn about python program to print all alphabets from a to z in lowercase or uppercase.
Getting Started
The task is to print all alphabets using python. Alphabets 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
Basically, we have to print above string in python.
We can do above task as below –
- Using String
- Using for loop
- Using while loop
- Using String Constant – ascii_lowercase or ascii_uppercase
Print Alphabets Using String
Since we know that there are only 26 letters of alphabets, we can hardcode the values in string and print them. For example,
-
Print Alphabets in Lowercase
def printAlphabets(): str = "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" print(str)
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
-
Print Alphabets in Uppercase
def printAlphabets(): str = "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" print(str)
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
In programming world, it is NOT recommended to hardcode the value. But, we can do so when we are dealing with facts. For example, There are only 10 digits, 26 letters in alphabets etc. In this scenario, we can do so.
If you don’t want to hardcode the values, continue reading to learn how to print alphabets in other ways.
Print Alphabets Using For Loop
We can print all alphabets using for loop –
Pseudo Algorithm
- As we already know, there are only 26 letters of alphabets. ASCII value of lowercase letter a is 97 and that of uppercase letter is 65
- Now, we run a for loop 26 times.
- In each iteration, get ASCII value of each letter. Then, get corresponding letter using chr() function.
- After that, print each letter using print() function.
Program to Print Alphabets in Lowercase
We can do so in many ways –
-
Method 1
def printLowercase(): for i in range(26): print(chr(97 + i), end = " ") printLowercase()
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
In above program,
- We know that there are only 26 letters in alphabet. So, we can run a for loop 26 times
- In each iteration, 97 + i provides us ASCII value of each letter starting from a.
- chr() returns character corresponding to given integer. So, chr(97 + i) will provide us letters of alphabets. value of i will decide which letter will be returned. For example, If i = 0, chr(97 + i) will be chr(97). So, letter a will be returned.
-
Method 2
range() can be used to iterate between given interval. As we already know that ASCII value of lowercase alphabets lie between 97 and 122. So, using this fact, we can print all alphabets as shown below –
for char in range(97, 123): print(chr(char), 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
In above program,
- We simply run a for loop from 97 till 122 using range() function.
- Then, we get corresponding character using chr() function and print it using print() function.
Program to Print Alphabets in Uppercase
Like Lowercase alphabets, we can do so in many ways –
-
Method 1
def printUppercase(): for i in range(26): print(chr(65 + i), end = " ") printUppercase()
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
In above program,
- We run for loop 26 times.
- In each iteration, get ASCII value using 65 + i. then, get corresponding character using chr() function.
- After that, print character using print() function.
-
Method 2
for char in range(65, 91): print(chr(char), 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
In above program,
- ASCII value of uppercase letter ranges from 65 to 90.
- Run a for loop using range(65, 91).
- Then, get corresponding character using chr() function and print it using print() function.
Print Alphabets Using while Loop
We can also python program to print alphabets in a to z using while loop –
Pseudo Algorithm
- Fact: Total letters in alphabets are 26, ASCII value of lowercase a is 97 and that of uppercase letter is 65.
- Run a while loop 26 using some counter variable.
- In each iteration, get character using chr() function.
- Print each character using print() function.
Print Alphabets in Lowercase
def printLowercase(): i = 0 while i < 26: print(chr(97 + i), end = " ") i = i + 1 printLowercase()
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
In above program,
- We have used counter variable i starting from 0.
- We run while loop 26 times. In each iteration, we get ASCII value of letter using 97 + i.
- Once we have ASCII value, we print character using chr() function. Then, increase the value of i by 1.
Print Alphabets in Uppercase
def printUppercase(): i = 0 while i < 26: print(chr(65 + i), end = " ") i = i + 1 printUppercase()
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
In above program,
- Create a counter variable i starting from 0.
- Run a while loop 26 times.
- In each iteration, get ASCII value using 65 + i. Then, print character using chr() function. Then, increase the value of i by 1.
Print Alphabets – ascii_lowercase or ascii_uppercase
String constants ascii_lowercase and ascii_uppercase returns alphabets in lowercase and uppercase respectively. Using this fact, we can print all alphabets –
Print in Lowercase – string.ascii_lowercase
We can print alphabets as shown below –
Method 1
import string def printLowercase(): res = string.ascii_lowercase[:26] print(res) printLowercase()
Output:
abcdefghijklmnopqrstuvwxyz
In above program,
- ascii_lowercase[:26] returns all alphabets in lowercase.
- Returned value is stored in variable – res
- Then, print returned value using print() function.
Since we want to print all letters of alphabets, we can also omit [:26] from ascii_lowercase. Hence, our program will be –
import string def printLowercase(): res = string.ascii_lowercase print(res) printLowercase()
Output:
abcdefghijklmnopqrstuvwxyz
Method 2
As we already know that ascii_lowercase returns all alphabets in lowercase, we can use it to run a for loop and print each character one by one –
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
Print in Uppercase – string.ascii_uppercase
We can also use string.ascii_uppercase to print all alphabets in uppercase as shown below –
-
Method 1
import string def printUppercase(): res = string.ascii_uppercase[:26] print(res) printUppercase()
Output:
ABCDEFGHIJKLMNOPQRSTUVWXYZ
In above program,
- ascii_uppercase returns all alphabets in uppercase. Writing ascii_uppercase[:26] means get first 26 characters from returned value. This value is stored in res variable.
- After that, value of res variable is printed using print() function.
Since we are trying to print all characters returned by ascii_uppercase, we can also omit [:26] as shown below –
import string def printUppercase(): res = string.ascii_uppercase print(res) printUppercase()
Output:
ABCDEFGHIJKLMNOPQRSTUVWXYZ
-
Method 2
We can do the same task with ascii_uppercase and for loop –
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
That’s how we can write Python Program to Print All Alphabets from a to z in lowercase or uppercase.
Reference: Official Doc