Python Practice › Loop Programs › Program 13
Print A to Z with chr()
Write a Python program to print the alphabet from A to Z using a for loop and chr() (chr(65) is ‘A’).
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 97 -> abcdefghijklmnopqrstuvwxyz
Approach
- Loop over the character codes 65 to 90 and convert each with
chr().
Solution
for code in range(65, 91): print(chr(code), end=" ")print()print(ord("a"), "->", "".join(chr(c) for c in range(ord("a"), ord("z") + 1)))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 97 -> abcdefghijklmnopqrstuvwxyz
Line by line
Line 1
65 is the Unicode code of ‘A’ and 90 of ‘Z’.
Line 4
ord() is the reverse of chr(); this line builds a to z without remembering the numbers.
Learn the concept
New to this topic? Read the lesson, then come back.