Python Practice › Loop Programs › Program 89

Uppercase letter diamond

MediumLoop Programs · 89 of 90

Write a Python program to print the letters A to Y in rows of 1, 2, 3, 4, 5, 4, 3, 2, 1 letters.

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

Approach

  1. Keep a letter counter and print the right number of letters on each row.

Solution

Python
sizes = [1, 2, 3, 4, 5, 4, 3, 2, 1]code = ord("A")for size in sizes:    row = [chr(code + i) for i in range(size)]    print(" ".join(row))    code += size
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

Line by line

Line 2

ord("A") is 65; adding to it walks through the alphabet.

Line 4

Each row takes the next size letters.

Learn the concept

New to this topic? Read the lesson, then come back.

More loop programs

← All 90 Loop Programs

Leave a Comment