Centered Alphabet Pyramid (Row-wise Letters) in Python

What You'll Learn
This program prints a centered pyramid: row 1 has one letter, row 2 has two, and so on. Letters form one continuous sequence from A (e.g. A, then B C, then D E F, …) using nested loops instead of hardcoded ASCII values like 64 or 69.
⭐ Pattern Output
When you run the program with rows = 5:
A
B C
D E F
G H I J
K L M N OComplete Python Program
Fixed rows = 5 version (same logic as the classic k / i / j loop, using ord('A') instead of magic numbers):
rows = 5
base = ord('A')
top = base + rows - 1 # 'E' when rows = 5
code = base - 1 # same idea as starting k at 64 before first print
for i in range(base, top + 1):
for j in range(top, base - 1, -1):
if j <= i:
code += 1
print(chr(code), end=" ")
else:
print(" ", end=" ")
print()🧠 How It Works
Running letter counter
code = base - 1 mirrors starting at 64 before the first increment. Each time a letter is printed, code += 1 and chr(code) yields the next character in the sequence.
Outer loop: row threshold i
for i in range(base, top + 1) walks from A to the top letter. Larger i means more positions satisfy j <= i, so each row prints more letters.
Inner loop: scan right to left
for j in range(top, base - 1, -1) visits column codes from the top letter down to A. If j > i, print a padded space; otherwise print the next letter. After the inner loop, print() starts the next row.
Centered growing rows
Row lengths are 1, 2, …, n; total letters are n(n+1)/2. Time is O(n²) for n rows; extra space is O(1).
Variation — User Input Version
Read rows from input. Clamped to 1–6 so total letters n(n+1)/2 stay within A–Z (6 rows → 21 letters):
rows = int(input("Enter number of rows (max 6): "))
rows = max(1, min(rows, 6))
base = ord('A')
top = base + rows - 1
code = base - 1
for i in range(base, top + 1):
for j in range(top, base - 1, -1):
if j <= i:
code += 1
print(chr(code), end=" ")
else:
print(" ", end=" ")
print()💡 Tips for Enhancement
Try These
- Use lowercase by setting
base = ord('a') - Collect each row in a list and
jointo control spacing without double-width padding - If you need more rows past the alphabet, wrap with modulo on the letter index
Avoid
- Hardcoding
64,65, or69instead oford('A') - Letting
rowsgrow until you print pastZwithout wrapping
Key Takeaways
top = ord('A') + rows - 1 replaces the old upper ASCII bound.
When j <= i, emit the next letter; otherwise emit padding — that centers the pyramid.
Total letters = n(n+1)/2; cap rows so you stay within A–Z.
❓ Frequently Asked Questions
j lays out columns from high code to low. While j > i you print padding; from j <= i onward you print the next letters, which produces the centered step pattern.ord('A') documents intent and works if you switch to another alphabet or encoding style; magic numbers are harder to maintain.Next: Python Alphabet Pattern 23
Continue to Program 23 for the next alphabet pattern in Python.
10 people found this page helpful
