Problem Formulation and Solution Overview
Ms. Smith, a Grade 2 teacher at Oakwood Public School, wants to strengthen her student’s Alphabet skills and needs your help. She would like a script that does the following:
- First, generate a list of the Alphabet in upper and lower case.
- Next, display the upper case version with the lower case directly below.
- Finally, show the characters evenly spaced out.
π¬ Question: How would we write the Python code to accomplish this task?
We can accomplish this task by one of the following options:
- Method 1: Use
ascii_uppercaseandascii_lowercase - Method 2: Use List Comprehension,
chr(),range()andord() - Method 3: Use List,
map()andrange() - Method 4: Use a
forLoop,range()andord()
Add the following code to the top of each code snippet. This snippet will allow the code in this article to run error-free.
import string
Method 1: Use ascii_uppercase and ascii_lowercase
The ascii_uppercase/ascii_lowercase methods return the respective case versions of the Alphabet. These values are immutable and return the same in any locale.
upperc = list(string.ascii_uppercase)
lowerc = list(string.ascii_lowercase)
frmt = "{:>5}"*26
print(frmt.format(*upperc))
print(frmt.format(*lowerc))Knowing this, we could send the output to the terminal as is.
Unformatted Output (Snippet)
['A', 'B', 'C', 'D', 'E', 'F', β¦] |
However, Ms. Smith would prefer that the output not contain quotes and be evenly spaced out. This can be accomplished by applying format() to the and upperclowerc variables.
Formatted 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 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 |
Perfect!
Method 2: Use List Comprehension, chr(), range() and ord()
Using List Comprehension, the following generates upper and lower case versions of the Alphabet by passing ord() a single character and returning a Unicode value. This value then converts to a character.
The range for A-Z is 65-90, and a-z is 97-122.
upperc = [chr(value) for value in range(ord('A'), ord('Z') + 26)]
lowerc = [chr(value) for value in range(ord('a'), ord('a') + 26)]
frmt = "{:>5}"*26
print(frmt.format(*upperc))
print(frmt.format(*lowerc))The upper and lower case Alphabets are created based on the above ranges and save to /upperclowerc respectively.
Formatted Output
|
Method 3: Use List, map() and range()
The map() function converts each value in the selected range to a character and returns an object. This object is then converted to a List.
upperc = list(map(chr, range(65, 91)))
lowerc = list(map(chr, range(97, 123)))
frmt = "{:>5}"*26
print(frmt.format(*upperc))
print(frmt.format(*lowerc))The upper and lower case Alphabets are created based on the above ranges and save to /upperclowerc respectively.
Formatted Output
|
Method 4: Use a For Loop, range() and ord()
The range() function combined with ord() produces a numeric Unicode for each value in the selected range. This value converts to the related character and appends to the appropriate list.
upperc = []
lowerc = []
for i in range(ord('A'), ord('Z') + 1):
upperc.append(chr(i))
for i in range(ord('a'), ord('z') + 1):
lowerc.append(chr(i))
frmt = "{:>5}"*26
print(frmt.format(*upperc))
print(frmt.format(*lowerc))The upper and lower case Alphabets are created based on the above ranges and save to /upperclowerc respectively.
Formatted 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 Za 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 |
Summary
These four (4) methods of generating the Alphabet should give you enough information to select the best one for your coding requirements.
Good Luck & Happy Coding!