Alphabet Pattern (A, ABC, ABCDE, ...) in Python

What You'll Learn
This Python program prints an alphabet pattern where each row starts at A and prints an odd number of letters: 1, 3, 5, 7, and so on.
⭐ Pattern Output
When you run the program with rows = 5:
A
ABC
ABCDE
ABCDEFG
ABCDEFGHIComplete Python Program
Fixed rows = 5 version:
rows = 5
base = ord('A')
for row in range(rows): # 0..4
letters = 2 * row + 1
for code in range(base, base + letters):
print(chr(code), end="")
print()🧠 How It Works
Odd-length rows
For row in 0..rows-1, letters = 2 * row + 1 yields widths 1, 3, 5, 7, 9, ….
Always start from A
The inner loop for code in range(base, base + letters) prints consecutive letters starting at A for every row, so each line is a fresh prefix of the alphabet.
End the row
Letters use end=""; print() after the inner loop adds the newline.
Odd pyramid
Total letters are 1+3+5+… = n² for n rows, so time is O(n²) and extra space is O(1).
Variation — User Input Version
Read rows from user input (clamped so we donโt go beyond Z):
rows = int(input("Enter rows (max 13): "))
rows = max(1, min(rows, 13))
base = ord('A')
for row in range(rows):
letters = 2 * row + 1
for code in range(base, base + letters):
print(chr(code), end="")
print()💡 Tips for Enhancement
Try These
- Switch to lowercase by using
ord('a') - Add spaces between letters for readability
- Center the rows to create a pyramid shape
- Print only vowels or skip letters for a variation
Avoid
- Hardcoding ASCII values like
66or76 - Letting user input exceed the alphabet without handling it
- Forgetting the newline after each row
Key Takeaways
Row r prints 2r + 1 letters.
Each row starts from A.
Prefer ord()/chr() over ASCII numbers.
❓ Frequently Asked Questions
rows. Row i (0-based) prints 2*i+1 letters, so the longest row prints 2*(rows-1)+1 letters.n rows, because the total printed characters grow roughly with n².Next: Python Alphabet Pattern 15
Continue to Program 15 for the next alphabet pattern in Python.
10 people found this page helpful
