I am new to python and did not see this exact questions or answers that will work. I am using Python 3.7 and simply need to display the character codes for uppercase alphabet letters (A-Z). I have that part, but it also requires the output be printed so each letter and character code appear on a separate line divided into two columns with a label.
My code works great but prints horizontally (which I prefer, but the instructions are to print vertically with a header).
def uppercaseAlphabets():
# uppercase
for c in range(65, 91):
print(chr(c), end=" ");
print("");
# Function to print the alphabet upper case codes
def uppercaseCodes():
for c in range(65, 91):
print((c), end=" ");
print("");
print("Uppercase Alphabets");
uppercaseAlphabets();
print("Uppercase Codes ");
uppercaseCodes();
The result is:
Uppercase Alphabet
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
Uppercase Codes
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
I want it to look like this:
Uppercase Alphabet Uppercase Codes
A 65
B 66
C 67
and so on. Any tweeks to my code are appreaciated. Thanks
Use formatted print output:
print(f"{'Uppercase Alphabet':^20}{'Uppercase Codes':^20}")
# try to avoid magic numbers, in 20 days it will be unclear why range(65, 91) is used
# you can get the code from the letter to make it clearer to understand after years
# you need ord("Z")+1 as range does not provide the upper limit
for code in range(ord("A"), ord("Z")+1):
print(f"{chr(code):^20}{code:^20}")
Output:
Uppercase Alphabet Uppercase Codes
A 65
B 66
C 67
D 68
E 69
F 70
G 71
H 72
I 73
J 74
K 75
L 76
M 77
N 78
O 79
P 80
Q 81
R 82
S 83
T 84
U 85
V 86
W 87
X 88
Y 89
Z 90
Documentation: