Alphabet Pattern (A to ABCDE) in Python

What You'll Learn
This Python program prints an alphabet pattern where each row starts at A and grows by one letter.
For n rows, the total letters printed are 1 + 2 + … + n = n(n + 1)/2.
⭐ Pattern Output
When you run the program with rows = 5:
A
AB
ABC
ABCD
ABCDEComplete Python Program
Fixed rows = 5 version (no magic ASCII numbers):
rows = 5
start = ord('A')
for i in range(1, rows + 1):
for code in range(start, start + i):
print(chr(code), end="")
print()🧠 How It Works
Outer loop: one row per i
for i in range(1, rows + 1) runs once per row. Row i prints exactly i letters.
Letter codes with ord() / chr()
start = ord('A') converts A into a number. The inner loop walks from start to start + i - 1, and chr(code) converts each number back to a letter.
Finish the row
print(chr(code), end="") appends letters on the same line, and the final print() adds a newline so the next row starts on a fresh line.
Growing alphabet rows
Total prints are 1+2+…+n = n(n + 1)/2, so runtime is O(n²) and extra space is O(1).
Variation — User Input Version
Read rows from user input (clamped to 26 to stay within A–Z):
rows = int(input("Enter the number of rows (max 26): "))
rows = max(1, min(rows, 26))
start = ord('A')
for i in range(1, rows + 1):
for code in range(start, start + i):
print(chr(code), end="")
print()💡 Tips for Enhancement
Try These
- Start from a different letter (e.g.,
'C') - Print lowercase output by using
ord('a') - Add spaces between letters (e.g., print with
end=" ") - Try a centered alphabet pyramid next
Avoid
- Using magic numbers like
65whenord('A')is clearer - Allowing
rows > 26without handling wrap-around - Forgetting a newline after each row
- Mixing tabs/spaces inconsistently in Python examples
Key Takeaways
Use nested loops: outer for rows, inner for letters.
Row i prints letters from A to the i-th letter.
Total printed letters are \(n(n+1)/2\), so runtime is O(n²).
Prefer ord()/chr() over raw ASCII numbers.
Clamp user input to 26 rows to stay within A to Z.
❓ Frequently Asked Questions
ord('A') to ord('a') (and keep the 26-row cap).print(chr(code), end=" ") inside the inner loop, then print a newline after the row.n rows, because the program prints 1+2+…+n letters in total.Next: Python Alphabet Pattern 2
Continue to Program 2 for the next alphabet pattern in Python.
In Python, ord() and chr() are the go-to tools for converting between letters and numeric codes while generating patterns.
10 people found this page helpful
