Widening Alphabet Triangle in Python

What You’ll Learn
This program prints a triangle where each row uses the next letter, centered with leading spaces. Row 0 is a single A; later rows print the same letter twice with a widening gap (B B, C C, …).
⭐ Pattern Output
When you run the program with rows = 5:
A
B B
C C
D D
E EComplete Python Program
Fixed rows = 5 version (no magic ASCII values):
rows = 5
rows = max(1, min(rows, 26))
base = ord('A')
for r in range(rows):
print(" " * (rows - 1 - r), end="")
ch = chr(base + r)
print(ch, end="")
if r > 0:
print(" " * (2 * r - 1), end="")
print(ch, end="")
print()🧠 How It Works
Letter for each row
Row index r uses chr(ord('A') + r): A, B, C, …
Center with leading spaces
" " * (rows - 1 - r) shifts the row right so the shape lines up under the apex.
Second letter and gap
For r > 0, print 2 * r - 1 spaces, then the same letter again. Row 0 skips this so you get a single A.
Widening mirrored triangle
The gap grows linearly with r; total characters per row are O(r), so overall time is O(n²) for n rows.
Variation — User Input Version
Read rows from input (clamped to 26):
rows = int(input("Enter number of rows (max 26): "))
rows = max(1, min(rows, 26))
base = ord('A')
for r in range(rows):
print(" " * (rows - 1 - r), end="")
ch = chr(base + r)
print(ch, end="")
if r > 0:
print(" " * (2 * r - 1), end="")
print(ch, end="")
print()💡 Tips for Enhancement
Try These
- Start from
ord('a')for a lowercase triangle - Print dots or dashes in the gap instead of spaces
- Mirror the triangle downward to form an hourglass
Avoid
- Hardcoding ASCII codes like
65and70 - Printing the second letter on row 0 (breaks the single-
Aapex)
Key Takeaways
Leading spaces: rows - 1 - r.
Gap width for r > 0: 2 * r - 1.
Overall time O(n²) for n rows.
❓ Frequently Asked Questions
r = 0 is the tip: print spaces, then A once. The second copy of the letter starts when r > 0, when there is room between the two sides.r, after the first letter print 2 * r - 1 spaces, then the same letter again. That yields gaps of 1, 3, 5, 7, … as r grows.n rows because the work in each row grows with the row index.Next: Python Alphabet Pattern 34
Continue to Program 34 for the next alphabet pattern in Python.
10 people found this page helpful
