Python Inverted Alphabet Triangle Pattern

Beginner
6 min read
Updated: Sep 2026
3 programs
Live preview

What Is This Pattern?

A decreasing alphabet pattern is Program 1 flipped: each row still prints letters forward from A, but row length shrinks from n down to 1.

Remember
Rule: for i = n..1, print A .. (A+i-1)

ABCDE
ABCD
ABC
AB
A       ← 5 rows

Compare Program 1 (grows A, AB, ABC) and Program 4 (counts down to A: A, BA, CBA). Program 6 keeps full width while the start letter moves forward.

How to Solve It

Outer loop shrinks the letter count; inner loop still prints A forward for that many letters.

MethodIdeaBest for
Nested ord/chrrange(rows, 0, -1) + range(base, base + i)Learning, interviews, exams
String sliceprint(letters[:i]) while i shrinksCompact shortcut once the idea clicks

Pseudocode

Pseudocode
base = 'A'
for i from rows down to 1:           // letter count
    for code from base to base+i-1:  // A forward
        print code
    print newline

Cheat sheet

GoalPattern
Shrink rowsfor i in range(rows, 0, -1):
Print A..lengthfor code in range(base, base + i):
Print letterprint(chr(code), end="")
End the rowprint()
Slice shortcutprint(letters[:i])
vs Program 1Same inner loop; outer goes down instead of up
vs Program 4Here letters ascend; Program 4 descends to A

Printing Letters vs Starting a New Line

APIEffectUse for
print(chr(code), end="")Stays on the same rowEach letter in the ascending run
print()Ends the current rowAfter the inner loop

Print letters without a newline, then end the row once.

Live Preview

Change the row count and the decreasing pattern updates instantly — first row is always the longest.

Whole numbers from 1 to 10. Size 5 means the first row is ABCDE. Tap a chip or type a value — the preview redraws as you go.

Live result 5 rows · first ABCDE
ABCDE
ABCD
ABC
AB
A

Worked Walkthrough — rows = 3

Outer i walks 3 → 2 → 1. Each row prints A forward for i letters.

iInner runPrinted row
3A..CABC
2A..BAB
1A..AA

Total letters are n+(n-1)+…+1 = n(n+1)/2 → O(n²).

Python Programs

Three complete programs: fixed height, row-count input(), and a slice shortcut. Use View Output for sample results.

Example 1 — Fixed rows = 5

Outer loop shrinks the length; inner loop prints A forward.

Python
rows = 5
base = ord("A")

for i in range(rows, 0, -1):
    for code in range(base, base + i):
        print(chr(code), end="")
    print()

How It Works

1. Shrink the count. range(rows, 0, -1) yields 5, 4, 3, 2, 1.

2. Print A forward. When i = 5, range(base, base + 5) prints ABCDE; when i = 1, just A.

3. Newline. Bare print() after the inner loop starts the next shorter row.

Example 2 — Row Count Input

Read how many rows to print. Cap at 26 so letters stay in A–Z.

Python
raw = input("Enter the number of rows (max 26): ").strip()
try:
    rows = int(raw)
except ValueError:
    print("Please enter a whole number.")
    raise SystemExit(1)

rows = max(1, min(rows, 26))
base = ord("A")

for i in range(rows, 0, -1):
    for code in range(base, base + i):
        print(chr(code), end="")
    print()

How It Works

1. Validate rows. Parse an integer, then clamp to 1..26.

2. Same shrinking core. Four rows start at ABCD and end at A.

Example 3 — letters[:i]

Slice the alphabet for each shrinking length — no inner letter loop.

Python
rows = 5
letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"

for i in range(rows, 0, -1):
    print(letters[:i])

How It Works

1. One slice. letters[:i] is the first i letters (ABCDE when i = 5).

2. Same shape. Identical output to Example 1 — keep the nested-loop version for exams that ask for ord/chr bounds.

Edge Cases & Pitfalls

Check these before calling the solution done.

grow not shrink

range(1, rows + 1)

That outer loop is Program 1. Keep range(rows, 0, -1) for the decreasing shape.

off-by-one

range(rows, -1, -1)

Including 0 makes an empty last row. Stop at 1 with range(rows, 0, -1).

newline early

Bare print() inside the inner loop

Use end="" for letters; call bare print() only after the ascending run.

rows > 26

Letter past Z

Cap rows at 26 so base + i - 1 stays in A–Z.

rows = 1

Single A

Output is just A — a good sanity check.

Bad input

Non-integer rows

Catch ValueError from int(...) before the outer loop.

Time and Space Complexity

ProgramTimeExtra space
Nested loops (Examples 1–2)O(n²)O(1)
Slice shortcut (Example 3)O(n²)O(n) per row for the slice string

There are n rows printing n+(n-1)+…+1 letters, so total work is n(n+1)/2.

Key Takeaways

  • Rule: for i = n..1, print A through the i-th letter.
  • Flip of Program 1: same ascending letters; only the outer loop direction changes.
  • Shortcut: letters[:i] inside a shrinking outer loop.
  • Next step: Program 6 keeps full width while the start letter moves toward E.

One line: for each length from n down to 1, print A forward that many letters.

Frequently Asked Questions

The outer loop walks i from rows down to 1, so the first row is longest. The inner loop prints letters from ord('A') through i characters: ABCDE, then ABCD, then ABC, and so on until A.
That range yields rows, rows-1, ..., 1 — exactly how many letters each row needs. Program 1 uses range(1, rows + 1) for the opposite (growing) shape.
print(chr(code), end="") stays on the same line. print() ends the current line. Letters use end=""; the row break uses print() after the inner loop.
Program 1 grows each row (A, AB, ABC). This pattern shrinks: the first row has rows letters from A, each next row drops the last letter. Only the outer loop direction changes — inner letter logic stays the same.
O(n²) where n is the number of rows. Total printed characters still equal n+(n-1)+...+1 = n(n+1)/2 — same triangular count as the increasing triangle.
Yes. Keep letters = "ABCDEFG..." and print(letters[:i]) inside for i in range(rows, 0, -1). Nested ord/chr loops teach the bounds; slicing is a compact shortcut.
Use try/except ValueError around int(input()), then clamp rows between 1 and 26 so letter codes stay within A–Z.
Letter codes walk past Z and print unexpected characters. Clamp to 26 for A–Z demos, or define a clear error/wrap policy.

Did you know?

Row i prints letters from A through the i-th letter, with i shrinking each row: ABCDE, ABCD, …, A. Total letters for n rows is still n(n+1)/2 — O(n²).

Next: Alphabet Pattern ABCDE to E

Keep a fixed end letter while the start letter moves forward each row.

Program 6 tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

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.

12 people found this page helpful