Right-Aligned Alphabet Triangle in Python

Beginner
⏱️ 6 min read
📚 Updated: Aug 2025
🎯 2 Code Examples
right-aligned

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:

Output
    A
   A B
  A B C
 A B C D
A B C D E
1

Complete Python Program

Fixed rows = 5 version (cleaned from the old ASCII loop; uses ord()/chr()):

Python
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

1

Row counter

row goes from 1 to rows, so each row prints one more letter than the previous.

Outer
2

Indentation

" " * (rows - row) prints leading spaces. As the row number grows, the indentation shrinks.

Align
3

Letters A to current

The inner loop prints codes from A up to the current row length and converts each code with chr(code).

Inner
=

Right-aligned triangle

Total letters are n(n+1)/2, so time is O(n²) and extra space is O(1).

2

Variation — User Input Version

Read rows from input (clamped to 26):

Python
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 rows exceed 26 without wrapping past Z

Key Takeaways

1

Indentation controls the alignment of the triangle.

2

Row r prints r letters from A upward.

3

Total letters are n(n+1)/2.

❓ Frequently Asked Questions

Use two spaces per indentation step and keep a single space between letters, or build each line as a string and pad it with rjust.
Yes. Replace base = ord('A') with another starting code like ord('C').
It’s O(n²) for n rows.

Next: Python Alphabet Pattern 28

Continue to Program 28 for the next alphabet pattern in Python.

Program 28 →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

10 people found this page helpful