Right-Aligned Alphabet Triangle in Python

What You'll Learn
This program prints A, then A B, then A B C, and so on, while adding leading spaces so the triangle is right-aligned.
⭐ Pattern Output
When you run the program with rows = 5:
A
A B
A B C
A B C D
A B C D EComplete Python Program
Fixed rows = 5 version (cleaned from the old ASCII loop; uses ord()/chr()):
rows = 5
rows = max(1, min(rows, 26))
base = ord('A')
for row in range(1, rows + 1):
# Indent to right-align the triangle
print(" " * (rows - row), end="")
for code in range(base, base + row):
print(chr(code), end=" ")
print()🧠 How It Works
Row counter
row goes from 1 to rows, so each row prints one more letter than the previous.
Indentation
" " * (rows - row) prints leading spaces. As the row number grows, the indentation shrinks.
Letters A to current
The inner loop prints codes from A up to the current row length and converts each code with chr(code).
Right-aligned triangle
Total letters are n(n+1)/2, so time is O(n²) and extra space is O(1).
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 row in range(1, rows + 1):
print(" " * (rows - row), end="")
for code in range(base, base + row):
print(chr(code), end=" ")
print()💡 Tips for Enhancement
Try These
- Print lowercase by starting from
ord('a') - Replace the trailing space with a join:
\" \".join(...) - Center-align by printing two spaces per indent step
Avoid
- Hardcoding ASCII values like 65 or 70
- Letting
rowsexceed 26 without wrapping past Z
Key Takeaways
Indentation controls the alignment of the triangle.
Row r prints r letters from A upward.
Total letters are n(n+1)/2.
❓ Frequently Asked Questions
rjust.base = ord('A') with another starting code like ord('C').n rows.Next: Python Alphabet Pattern 28
Continue to Program 28 for the next alphabet pattern in Python.
10 people found this page helpful
