Print A to Z with chr()
EasyLoop Programs · 13 of 90
Write a Python program to print the alphabet from A to Z using a for loop and chr() (chr(65) is ‘A’).
OutputA 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
Python
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 165 is the Unicode code of ‘A’ and 90 of ‘Z’.
Line 4ord() 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.
More loop programs
← All 90 Loop Programs