An alphabet X pattern places one letter per row on two diagonals of a fixed-width grid. Letters step A, B, C… as the diagonals meet at the center.
Remember
Rule: letter = A+r at columns left=r and right=width-1-r
width = 2*rows - 1
A A
B B
C C
D D
E ← 5 rows, width 9
On the last row left == right, so only one center letter prints. Compare Program 30 (mixed rows) and Program 32 (centered palindrome pyramid).
Approach
How to Solve It
For each row r, scan every column and print the row letter only on the two diagonal positions.
Method
Idea
Best for
Column scan
print(ch if c == left or c == right else " ", end="")
Learning diagonals and spaces
List + join
Append each cell, then "".join(row)
Debugging; inspect the row before print
Pseudocode
Pseudocode
width = 2 * rows - 1
for r from 0 to rows - 1:
ch = 'A' + r
left = r
right = width - 1 - r
for c from 0 to width - 1:
print (ch if c == left or c == right else space)
print newline
Cheat sheet
Goal
Pattern
Grid width
width = 2 * rows - 1
Walk rows
for r in range(rows):
Row letter
ch = chr(ord("A") + r)
Diagonals
left = r, right = width - 1 - r
Print cell
ch if c == left or c == right else " "
Center row
left == right → one letter only
End the row
print()
Printing Letters vs Starting a New Line
API
Effect
Use for
print(ch, end="") / print(" ", end="")
Stays on the same row
Each letter or space cell
print()
Ends the current row
After the column loop
Print cells without a newline, then end the row once.
Try it
Live Preview
Change the row count and the alphabet X updates instantly — including width and the center letter.
Whole numbers from 1 to 10. Size 5 means letters A–E and width 9. Tap a chip or type a value — the preview redraws as you go.
Live result5 rows · width 9
A A
B B
C C
D D
E
Trace
Worked Walkthrough — rows = 3
Width is 2×3 - 1 = 5. Trace letter and diagonal columns for each row.
r
Letter / columns
Printed row
0
A at 0 and 4
A A
1
B at 1 and 3
B B
2
C at 2 (center)
C
Each of n rows scans 2n-1 columns → O(n²).
Code
Python Programs
Three complete programs: fixed height, row-count input(), and a list-join style. Use View Output for sample results.
Example 1 — Fixed rows = 5
Scan each column; print the row letter only on the two diagonal positions.
Python
rows = 5
width = 2 * rows - 1
base = ord("A")
for r in range(rows):
ch = chr(base + r)
left = r
right = width - 1 - r
for c in range(width):
if c == left or c == right:
print(ch, end="")
else:
print(" ", end="")
print()
Output
A A
B B
C C
D D
E
How It Works
1. Set the grid.width = 9 for 5 rows. Letter for row r is chr(ord("A") + r).
2. Mark diagonals.left = r moves inward from the left; right = width - 1 - r from the right.
3. Print or space. Print ch when c hits either diagonal; otherwise print a space. On the last row left == right == 4 → one center E.
Example 2 — Row Count Input
Read how many rows to print. Cap at 26 so letters stay in A–Z.
Python
raw = input("Enter 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))
width = 2 * rows - 1
base = ord("A")
for r in range(rows):
ch = chr(base + r)
left = r
right = width - 1 - r
for c in range(width):
print(ch if (c == left or c == right) else " ", end="")
print()
Output (when user enters 3)
A A
B B
C
How It Works
1. Validate rows. Parse an integer, then clamp to 1..26.
2. Same diagonals. Three rows use width 5 with letters A–C — row 1 prints B at columns 1 and 3.
Example 3 — List Join
Collect each cell in a list, then print with "".join(row) — same shape, easier to inspect.
Python
rows = 5
width = 2 * rows - 1
base = ord("A")
for r in range(rows):
ch = chr(base + r)
left = r
right = width - 1 - r
row = []
for c in range(width):
row.append(ch if (c == left or c == right) else " ")
print("".join(row))
Output
A A
B B
C C
D D
E
How It Works
1. Build then print. The column loop appends to row instead of printing immediately.
2. Join once."".join(row) builds the full line — identical shape to Example 1.
Edge Cases & Pitfalls
Check these before calling the solution done.
wrong width
width = rows
Using only rows columns collapses the X. Keep 2 * rows - 1.
double center
Printing left and right separately on the last row
If you print left then right without checking equality, you can emit two center letters. The or condition naturally prints once when they meet.
newline early
Bare print() inside the column loop
Use end="" for each cell; call bare print() only after the row finishes.
rows > 26
Letter past Z
Cap rows at 26 so chr(ord("A") + r) stays in A–Z.
rows = 1
Single A
left == right == 0 → just A on one line.
Bad input
Non-integer rows
Catch ValueError from int(...) before computing width.
Analysis
Time and Space Complexity
Program
Time
Extra space
Inline print (Examples 1–2)
O(n²)
O(1)
List join (Example 3)
O(n²)
O(n) per row for the list
There are n rows and each scans 2n-1 columns, so total work is quadratic.
Remember
Key Takeaways
Diagonals: letter at left = r and right = width - 1 - r.
Width: always 2*rows - 1 so both sides of the center match.
Center: last row has left == right — one letter only.
Next step: Program 32 builds a centered alphabet palindrome pyramid.
One line: for each row r, print A+r at columns r and width-1-r, spaces elsewhere.
Frequently Asked Questions
Letters sit on two diagonals forming an X shape. Row r prints chr(ord('A') + r) at column left = r and column right = width - 1 - r inside a grid of width 2*rows - 1.
On the final row left equals right at the center column. The condition c == left or c == right is true for only one column, so one letter prints — not two.
For row index r (0-based), left = r moves down-right. right = width - 1 - r moves down-left. They converge at the center when r equals rows - 1.
The X needs equal space on both sides of the center. For rows=5 the widest row has A at columns 0 and 8, so width is 9 = 2*5 - 1.
Each row uses letters starting at A through the r-th letter. With rows > 26 you would need characters beyond Z unless you define a wrap policy.
print(ch, end="") keeps each character on the same row. print() ends the row after the inner column loop finishes.
O(n²) where n is rows. There are n rows and each row scans width = 2*n - 1 columns.
Use try/except ValueError around int(input()), then clamp rows between 1 and 26.
🤔
Did you know?
Row r prints letter chr(ord('A') + r) at columns left = r and right = width - 1 - r in a grid of width 2*rows - 1. On the last row both diagonals meet, so only one E appears in the center.