Continuous Alphabet Triangle (Decreasing Rows) in Python

What You'll Learn
This program prints a decreasing triangle where letters continue across rows: first 5 letters, then 4, then 3, then 2, then 1.
⭐ Pattern Output
When you run the program with rows = 5:
A B C D E\nF G H I\nJ K L\nM N\nOComplete Python Program
Fixed rows = 5 version (cleaned from the reference logic; no ASCII constants like 64):
rows = 5
code = ord('A')
for row_len in range(rows, 0, -1): # 5, 4, 3, 2, 1
for _ in range(row_len):
print(chr(code), end=" ")
code += 1
print()🧠 How It Works
One running letter counter
code = ord('A') stores the next letter to print. It is never reset, so letters continue from row to row.
Outer loop chooses row length
for row_len in range(rows, 0, -1) produces 5, 4, 3, 2, 1 for rows = 5. That’s why each next row is shorter.
Inner loop prints and increments
The inner loop runs row_len times. Each time it prints chr(code) and then does code += 1 to move to the next letter.
Continuous decreasing triangle
Total letters are n(n+1)/2, so runtime is O(n²) and 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))
code = ord('A')
for row_len in range(rows, 0, -1):
for _ in range(row_len):
print(chr(code), end=" ")
code += 1
print()💡 Tips for Enhancement
Try These
- Use lowercase by starting from
ord('a') - Start from another letter (like
ord('F')) to shift the sequence - Wrap after Z using modulo arithmetic if you want more rows
Avoid
- Hardcoding ASCII values like
64or70 - Letting rows exceed 6 without handling wrap-around
Key Takeaways
The outer loop sets row sizes as rows..1.
One counter prints a continuous alphabet across all rows.
Total letters are n(n+1)/2 — keep n small to stay within A–Z.
❓ Frequently Asked Questions
code = ord('A') inside the outer loop so it resets before printing each row.\" \".join(row_letters).n(n+1)/2.Next: Python Alphabet Pattern 26
Continue to Program 26 for the next alphabet pattern in Python.
10 people found this page helpful
