Python Program to Print an Alphabet Pattern
Beginner · Patterns · CBSE class 11 practical list
This program prints a right-angled triangle of capital letters: A on the first row, AB on the second, ABC on the third, and so on. It uses the fact that every character has a number code, so letters can be produced in a loop.
Program
alphabet_pattern.py
rows = 5 # or: rows = int(input("Enter the number of rows: "))
for i in range(1, rows + 1):
for j in range(i):
# chr(65) is "A", chr(66) is "B", and so on
print(chr(65 + j), end="")
print() # move to the next line
Output
A AB ABC ABCD ABCDE
How it works
chr()turns a character code into a character:chr(65)is"A",chr(66)is"B", and so on up tochr(90), which is"Z".- The outer loop picks the row, and the inner loop prints
iletters on it, starting from A each time. end=""keeps the letters of a row together, and the emptyprint()starts the next row.- The opposite of
chr()isord(), which gives the code of a character, soord("A")is 65.
Other ways to write it
Inverted alphabet triangle
Inverted alphabet triangle
rows = 5
for i in range(rows, 0, -1):
for j in range(i):
print(chr(65 + j), end="")
print()
Output
ABCDE ABCD ABC AB A
Counting the rows down from 5 prints ABCDE first and ends with a single A.
Same letter on each row
Same letter on each row
rows = 5
for i in range(rows):
letter = chr(65 + i)
print(letter * (i + 1))
Output
A BB CCC DDDD EEEEE
letter * (i + 1) repeats one letter, giving A, BB, CCC, DDDD and EEEEE.
Try it yourself
Change 65 to 97 to print the same pattern in small letters.
Edit the program above and press Run, or open it in the full editor to save it and share it. To watch it run line by line, open it in the visualizer.
Related programs
All Python programs · CBSE class 12 practical file · Python exercises