X Rule
Two diagonals
Each row prints one letter at left and right columns; all other columns are spaces.

Each row places one letter on two diagonals forming an X shape: row 0 prints A at both ends, each next row steps inward with B, C, D, until a single E lands in the center for five rows. A column loop with left = r and right = width - 1 - r teaches 2D grid thinking. Compare with star X pattern (0 and *). Includes a live preview, worked Python examples, edge cases, and complexity.
Two diagonals
Each row prints one letter at left and right columns; all other columns are spaces.
col = r
left = r moves one column right each row — top-left to center.
col = width - 1 - r
right = width - 1 - r moves one column left each row — top-right to center.
2*rows - 1
width = 2 * rows - 1 gives equal space on both sides of the center column.
1–26 rows
Pick a row count and draw the alphabet X pattern in the browser instantly.
Complexity
n rows × 2n - 1 columns per row ≈ O(n²); extra memory stays O(1).
An alphabet X pattern places letters on two diagonals inside a grid of width 2*rows - 1. Row 0 prints A at both ends; each row steps inward until the diagonals meet at one center letter.
In Python you solve it with an outer loop over row index r, an inner column loop, and ord()/chr() — or build each row in a list and print(''.join(row)) for clarity.
It teaches 2D grid coordinates — mapping row and column indices to print positions — the same skill used in matrices, game boards, and ASCII art. When left == right, both diagonals meet and only one character prints.
width = 2 * rows - 1 — for rows=5 the grid is 9 columns wide.
left = r — column index grows one step right each row.
right = width - 1 - r — column index shrinks one step left each row.
When left == right, only one letter prints — not two copies.
In short: set width = 2 * rows - 1 and base = ord('A'), loop r from 0 to rows - 1, compute left = r and right = width - 1 - r, print the row letter at those columns and spaces elsewhere, then print() for the newline.
Given a positive integer rows, print an X-shaped grid of rows lines, each 2*rows - 1 characters wide. Row r prints chr(ord('A') + r) at columns left = r and right = width - 1 - r; all other positions are spaces.
# First 5 rows (width = 9)
# A A
# B B
# C C
# D D
# E | Item | Type | Description |
|---|---|---|
rows | int | Number of rows (also the row letter runs A through the rows-th letter). Clamp to 1–26 for A–Z demos. |
| Printed output | text | X-shaped grid: letters on two diagonals, spaces elsewhere — width 2*rows - 1 per line. |
width = 2 * rows - 1
base = ord('A')
for r from 0 to rows-1:
ch = chr(base + 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 | Approach | Idea | Best for |
|---|---|---|
| Column loop | for c in range(width): print letter or space | Learning 2D grid coordinates and diagonals |
| Join list | Build row in a list, print(''.join(row)) | Clearer debugging and row inspection |
| Star X contrast | See Program 45 (0 and * X) | Same diagonal logic with symbols instead of letters |
| Goal | Pattern |
|---|---|
| Grid width | width = 2 * rows - 1 |
| Outer loop (row index) | for r in range(rows): |
| Row letter | ch = chr(base + r) |
| Diagonal columns | left = r; right = width - 1 - r |
| Column loop | for c in range(width): print(ch if c==left or c==right else " ", end="") |
| End the row | print() |
| List join variant | row.append(ch if ... else " "); print(''.join(row)) |
Three ways to think about the same X grid — pick based on what you are learning.
left = r
col moves rightTop-left to center — column index equals row index.
right = width-1-r
col moves leftTop-right to center — column index shrinks as rows grow.
row.append(...)
''.join(row)Collect characters in a list, then print one string — easier to inspect each row while debugging.
Reach for diagonal column loops when you need letters (or symbols) on two crossing lines inside a fixed-width grid.
Program 30 mixes prefix and suffix on one line. This pattern uses a 2D grid with diagonal columns.
Practice mapping (r, c) pairs to print positions before tackling matrices.
Same left/right column rule appears in star X patterns and hollow diamond shapes.
Next pattern builds a centered palindrome pyramid — another symmetric shape.
This is a console teaching pattern — not how you build modern app screens.
Key benefit: one program that proves you can map row/column indices to diagonal positions — a pattern used in matrices, game boards, and ASCII art far beyond alphabet demos.
Choose a row count between 1 and 26 and draw the alphabet X pattern in the browser.
Three complete Python programs — fixed five rows with diagonal column loops, console input with ternary form, and a list-join variant for clarity. Click View Output to reveal sample console results.
Print five rows of the alphabet X pattern with left/right diagonal column loops.
rows = 5Hard-coded height — ideal for first demos and screenshots.
rows = 5
rows = max(1, min(rows, 26))
width = 2 * rows - 1
base = ord('A')
for r in range(rows): # 0..rows-1
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() The outer loop walks row index r from 0 to 4. For each row, ch = chr(base + r) selects the letter and left = r, right = width - 1 - r mark the two diagonal columns. The inner loop scans every column: print the letter when c == left or c == right, otherwise print a space. On the last row left == right == 4, so only one E appears in the center.
Let the user choose the height at runtime.
Read rows and clamp to 1–26. Wrap int(input()) in try/except ValueError in real apps.
rows = int(input("Enter number of rows (max 26): "))
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() Same diagonal grid core as Example 1; only the row count comes from input. Three rows use width 5 with letters A–C on the diagonals — row 2 prints B at columns 1 and 3.
Build each row in a list, then print with ''.join(row).
''.join(row) VariantCollect letters in a list for clearer row inspection — same logic, easier debugging.
rows = 5
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
row = []
for c in range(width):
row.append(ch if (c == left or c == right) else " ")
print(''.join(row)) The column loop appends each character to row instead of printing immediately. ''.join(row) builds the full line — identical output to Examples 1 and 2, but you can inspect row before printing during debugging.
Clamp rows, then set width = 2 * rows - 1 and base = ord('A') for the grid and alphabet.
for r in range(rows): walks each row from 0 to rows - 1, computing left, right, and ch.
for c in range(width): prints the letter at diagonal columns and spaces elsewhere with print(..., end="").
print() ends the row after the column loop finishes; the outer loop advances r to the next row.
Total characters: n × (2n - 1) ≈ O(n²) — O(n²) time, O(1) extra memory (loop version).
rows = 5Trace each outer-loop value of r and see how left, right, and the row letter produce each printed line.
r | Letter | left | right | Row output |
|---|---|---|---|---|
| 0 | A | 0 | 8 | A A |
| 1 | B | 1 | 7 | B B |
| 2 | C | 2 | 6 | C C |
| 3 | D | 3 | 5 | D D |
| 4 | E | 4 | 4 | E (left==right) |
Total character prints: 5 × 9 = 45 = rows × width for rows = 5.
Where diagonal grid patterns show up beyond the homework prompt.
Program 30 mixes prefix and suffix on one line. This pattern uses a 2D grid with diagonal columns.
Example: compare fixed-width rows vs spaced X grid side by side.
Reinforce mapping (r, c) pairs to print positions before tackling matrices.
Example: trace left and right for row 2 (r=1) on paper before coding.
Same diagonal positions with * instead of letters — see Program 45.
Example: swap ch for * and keep the column loop.
Swap letters for digits 1..n at the same diagonal columns.
Example: rows=3 prints 1 at corners and 3 in the center.
Rectangular totals make O(n²) concrete for beginners.
Example: 5 rows → 45 characters printed (5 × 9).
Classic nested-loop question that tests diagonal column indices and center merge.
Example: explain why only one E prints when rows=5 without running code.
Pro Tip: say “letter at left and right columns, space everywhere else” before coding — that story prevents wrong width or missing spaces.
Why this pattern earns a spot after the mixed alphabet rows from Program 30.
Row and column loops map directly to matrix coordinates — a core programming skill.
The X is visually symmetric — left and right diagonals mirror each other.
Direct print version for learning; list-join version for clearer debugging.
Streaming output needs no storage beyond loop counters (join variant uses O(width) per row).
Pro Tip: when left == right on the last row, the or condition still prints exactly once per column — no special case needed.
Small habits that keep alphabet X pattern code clean.
Use left = r and right = width - 1 - r — keep r and c for loop variables.
int(input()) in try/exceptAvoid crashes when the user types letters instead of a number.
rows = max(1, min(rows, 26)) keeps demos inside A–Z.
width = 2 * rows - 1 before the row loop — do not recompute inside unless rows changes.
Trace A at corners, B, center C on paper before coding larger demos.
Pro Tip: if the shape looks like a solid block, you likely forgot spaces — only diagonal columns should print letters.
Mistakes that commonly break alphabet X patterns.
Using width = rows or width = 2 * rows misaligns the diagonals — the X looks skewed or truncated.
→ Use width = 2 * rows - 1 so the top row has equal space on both sides.
Some beginners add a separate branch when left == right and accidentally print the letter twice.
→ The condition c == left or c == right already prints once per column — no extra branch needed.
Magic numbers like chr(65 + r) work but break readability and lowercase variants.
→ Use base = ord('A') and chr(base + r) instead of raw ASCII values.
Non-numeric input raises ValueError with bare int(input()).
→ Wrap int(input()) in try/except ValueError and validate range.
Printing only letters without spaces collapses the X into a solid diagonal block.
→ The inner loop must print a space character for every non-diagonal column.
Check these inputs before calling the solution done.
Output is just A on one line — left == right == 0, so one letter at column 0.
Treat as invalid; re-prompt instead of silent empty output.
26 rows with width 51 — last row prints Z once at the center column.
Clamp to 26 or define a wrap/error policy before printing.
Use try/except ValueError before clamping rows.
Same loops work with base = ord('a') and lowercase output.
Try these variations to lock in the pattern.
row.append and ''.join(row)* and 0n rows is n × (2n - 1) — each row scans the full width.for c in range(width): with left = r and right = width - 1 - r.''.join(row) after building in a list is equivalent to the direct-print version — use whichever fits your lesson.Z once at the center when diagonals meet.Quick Takeaway: outer loop sets r, compute left and right, print the row letter at diagonal columns and spaces elsewhere, then break the line — that is the whole alphabet X pattern.
| Program | Time | Extra space |
|---|---|---|
| Column loop (Examples 1–2) | O(rows²) | O(1) |
| Join variant (Example 3) | O(rows²) | O(width) for the row list per line |
The alphabet X pattern teaches 2D grid thinking — mapping row and column indices to diagonal print positions. Master the direct-print version, then try the list-join variant for clearer debugging.
Practice the three examples above, then continue to Program 32 in the alphabet pattern series.
Set width, left, and right each row, run the column loop, clamp rows to 26, and compare with star X pattern (Program 45) using the same diagonal logic.
base = ord('A'), width = 2 * rows - 1left = r and right = width - 1 - r each rowprint(ch, end="") or ternary in the column loop; print() afterwidth formula — diagonals will not alignord('A')left == right — one column already prints onceprint() inside the letter loopsPrint the X grid the beginner-friendly way.
Two diagonals
Definitionleft = r
Codewidth - 1 - r
Code''.join(row)
AltO(n²) time
AnalysisRow 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.
Next up: the centered alphabet palindrome pyramid — build on symmetric shape ideas from this tutorial.
12 people found this page helpful