Widening Alphabet Triangle in Python

Beginner
⏱️ 6 min read
📚 Updated: Aug 2025
🎯 2 Code Examples
mirror, spacing

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:

Output
    A
   B B
  C   C
 D     D
E       E
1

Complete Python Program

Fixed rows = 5 version (no magic ASCII values):

\n Python
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

1

Letter for each row

Row index r uses chr(ord('A') + r): A, B, C, …

Outer
2

Center with leading spaces

" " * (rows - 1 - r) shifts the row right so the shape lines up under the apex.

Align
3

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.

Mirror
=

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.

2

Variation — User Input Version

Read rows from input (clamped to 26):

\n Python
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 65 and 70
  • Printing the second letter on row 0 (breaks the single-A apex)

Key Takeaways

1

Leading spaces: rows - 1 - r.

2

Gap width for r > 0: 2 * r - 1.

3

Overall time O(n²) for n rows.

❓ Frequently Asked Questions

Row 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.
For row index 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.
It’s O(n²) for 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.

Program 34 →

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