Centered Continuous Alphabet Pyramid in Python

Beginner
⏱️ 6 min read
📚 Updated: Aug 2025
🎯 2 Code Examples
odd rows, centered

What You'll Learn

This program prints a centered triangle of letters where each row has an odd count (1, 3, 5, …) and letters continue from A without restarting each row.

⭐ Pattern Output

When you run the program with rows = 3 (same shape as the classic 5-column version):

Output
    A
  B C D
E F G H I
1

Complete Python Program

Fixed rows = 3 version (generalized from the original 5-column loop; no magic ASCII numbers):

Python
rows = 3
width = 2 * rows - 1  # 5 when rows = 3 (matches original j from 4 down to 0)

code = ord('A') - 1  # increment before first print, same as starting k at 64

for i in range(0, width, 2):
    for j in range(width - 1, -1, -1):
        if j > i:
            print(" ", end="")
        else:
            code += 1
            print(chr(code), end=" ")
    print()

🧠 How It Works

1

Grid size

width = 2 * rows - 1 sets the horizontal span. The outer loop for i in range(0, width, 2) steps by 2 so each row prints an odd number of letters with increasing width.

Layout
2

Spaces vs letters

For each column index j from right to left, if j > i print a space (leading padding); otherwise bump code and print chr(code) with a trailing space.

Inner
3

Continuous alphabet

code starts at ord('A') - 1 and increments only when a letter is printed, so letters flow across rows without restarting at A.

State
=

Centered odd rows

Roughly O(width²) cells are visited per row count — O(rows²) time for this shape; O(1) extra space besides output.

2

Variation — User Input Version

Read rows from input. Clamped to 1–5 so total letters rows² stay within A–Z:

Python
rows = int(input("Enter number of rows (max 5): "))
rows = max(1, min(rows, 5))

width = 2 * rows - 1
code = ord('A') - 1

for i in range(0, width, 2):
    for j in range(width - 1, -1, -1):
        if j > i:
            print(" ", end="")
        else:
            code += 1
            print(chr(code), end=" ")
    print()

💡 Tips for Enhancement

Try These

  • Use lowercase by starting from ord('a') - 1
  • Build each line as a string with join to avoid trailing spaces
  • After 26 letters, wrap with chr(ord('A') + (code - ord('A')) % 26) if you allow more rows

Avoid

  • Hardcoding 64 instead of ord('A') - 1
  • Increasing rows past 5 without wrapping — you will run past Z

Key Takeaways

1

width = 2 * rows - 1 matches the original inner range (e.g. 5 columns).

2

When j > i, print a leading space; otherwise print the next letter.

3

Total letters = rows²; keep rows ≤ 5 for A–Z.

❓ Frequently Asked Questions

Each row prints an odd number of letters (1, 3, 5, …). Stepping i by 2 matches that growth while the inner loop still scans the full width for leading spaces.
Total letters are 1+3+5+…+(2n−1) = . For n = 6 that is 36 letters (past Z); n = 5 uses 25 letters.
It’s O(n²) for n rows, because width is ~2n and there are ~n rows.

Next: Python Alphabet Pattern 17

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

Program 17 →

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