Centered Continuous Alphabet Pyramid in Python

What You'll Learn
This program prints a centered triangle of letters where each row has an odd count (1, 3, 5, …) and letters continue from A without restarting each row.
⭐ Pattern Output
When you run the program with rows = 3 (same shape as the classic 5-column version):
A
B C D
E F G H IComplete Python Program
Fixed rows = 3 version (generalized from the original 5-column loop; no magic ASCII numbers):
rows = 3
width = 2 * rows - 1 # 5 when rows = 3 (matches original j from 4 down to 0)
code = ord('A') - 1 # increment before first print, same as starting k at 64
for i in range(0, width, 2):
for j in range(width - 1, -1, -1):
if j > i:
print(" ", end="")
else:
code += 1
print(chr(code), end=" ")
print()🧠 How It Works
Grid size
width = 2 * rows - 1 sets the horizontal span. The outer loop for i in range(0, width, 2) steps by 2 so each row prints an odd number of letters with increasing width.
Spaces vs letters
For each column index j from right to left, if j > i print a space (leading padding); otherwise bump code and print chr(code) with a trailing space.
Continuous alphabet
code starts at ord('A') - 1 and increments only when a letter is printed, so letters flow across rows without restarting at A.
Centered odd rows
Roughly O(width²) cells are visited per row count — O(rows²) time for this shape; O(1) extra space besides output.
Variation — User Input Version
Read rows from input. Clamped to 1–5 so total letters rows² stay within A–Z:
rows = int(input("Enter number of rows (max 5): "))
rows = max(1, min(rows, 5))
width = 2 * rows - 1
code = ord('A') - 1
for i in range(0, width, 2):
for j in range(width - 1, -1, -1):
if j > i:
print(" ", end="")
else:
code += 1
print(chr(code), end=" ")
print()💡 Tips for Enhancement
Try These
- Use lowercase by starting from
ord('a') - 1 - Build each line as a string with
jointo avoid trailing spaces - After 26 letters, wrap with
chr(ord('A') + (code - ord('A')) % 26)if you allow more rows
Avoid
- Hardcoding
64instead oford('A') - 1 - Increasing rows past 5 without wrapping — you will run past
Z
Key Takeaways
width = 2 * rows - 1 matches the original inner range (e.g. 5 columns).
When j > i, print a leading space; otherwise print the next letter.
Total letters = rows²; keep rows ≤ 5 for A–Z.
❓ Frequently Asked Questions
i by 2 matches that growth while the inner loop still scans the full width for leading spaces.n = 6 that is 36 letters (past Z); n = 5 uses 25 letters.n rows, because width is ~2n and there are ~n rows.Next: Python Alphabet Pattern 17
Continue to Program 17 for the next alphabet pattern in Python.
10 people found this page helpful
