Python Practice › Loop Programs › Program 13

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’).

Advertisement
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

  1. 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 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.

More loop programs

← All 90 Loop Programs

Leave a Comment