Centered Alphabet Pyramid (Row-wise Letters) in Python

Beginner
⏱️ 6 min read
📚 Updated: Aug 2025
🎯 2 Code Examples
nested loops, ord/chr

What You'll Learn

This program prints a centered pyramid: row 1 has one letter, row 2 has two, and so on. Letters form one continuous sequence from A (e.g. A, then B C, then D E F, …) using nested loops instead of hardcoded ASCII values like 64 or 69.

⭐ Pattern Output

When you run the program with rows = 5:

Output
        A
      B C
    D E F
  G H I J
K L M N O
1

Complete Python Program

Fixed rows = 5 version (same logic as the classic k / i / j loop, using ord('A') instead of magic numbers):

Python
rows = 5

base = ord('A')
top = base + rows - 1  # 'E' when rows = 5
code = base - 1        # same idea as starting k at 64 before first print

for i in range(base, top + 1):
    for j in range(top, base - 1, -1):
        if j <= i:
            code += 1
            print(chr(code), end=" ")
        else:
            print(" ", end=" ")
    print()

🧠 How It Works

1

Running letter counter

code = base - 1 mirrors starting at 64 before the first increment. Each time a letter is printed, code += 1 and chr(code) yields the next character in the sequence.

State
2

Outer loop: row threshold i

for i in range(base, top + 1) walks from A to the top letter. Larger i means more positions satisfy j <= i, so each row prints more letters.

Outer
3

Inner loop: scan right to left

for j in range(top, base - 1, -1) visits column codes from the top letter down to A. If j > i, print a padded space; otherwise print the next letter. After the inner loop, print() starts the next row.

Inner
=

Centered growing rows

Row lengths are 1, 2, …, n; total letters are n(n+1)/2. Time is O(n²) for n rows; extra space is O(1).

2

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):

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

base = ord('A')
top = base + rows - 1
code = base - 1

for i in range(base, top + 1):
    for j in range(top, base - 1, -1):
        if j <= i:
            code += 1
            print(chr(code), end=" ")
        else:
            print(" ", end=" ")
    print()

💡 Tips for Enhancement

Try These

  • Use lowercase by setting base = ord('a')
  • Collect each row in a list and join to control spacing without double-width padding
  • If you need more rows past the alphabet, wrap with modulo on the letter index

Avoid

  • Hardcoding 64, 65, or 69 instead of ord('A')
  • Letting rows grow until you print past Z without wrapping

Key Takeaways

1

top = ord('A') + rows - 1 replaces the old upper ASCII bound.

2

When j <= i, emit the next letter; otherwise emit padding — that centers the pyramid.

3

Total letters = n(n+1)/2; cap rows so you stay within A–Z.

❓ Frequently Asked Questions

Descending j lays out columns from high code to low. While j > i you print padding; from j <= i onward you print the next letters, which produces the centered step pattern.
ord('A') documents intent and works if you switch to another alphabet or encoding style; magic numbers are harder to maintain.
Each of n rows scans n columns — O(n²). Output length is n(n+1)/2 characters.

Next: Python Alphabet Pattern 23

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

Program 23 →

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