Pattern programs help improve logic and problem-solving in coding. Using Python loops and the ASCII system, we can print alphabet-based patterns like triangles, pyramids and more.
ASCII Important Points:
- Uppercase letters: A-Z: ASCII 65–90
- Lowercase letters: a-z: ASCII 97–122
- Use chr() to convert an ASCII value to its character (e.g., chr(65)- 'A').
Let's look at some common alphabet patterns:
1. Left Triangle Alphabet Pattern
Prints a left-aligned triangle using uppercase letters. Each row prints increasing characters starting from 'A'.
def left_triangle_pattern(rows):
for i in range(rows):
for j in range(i + 1):
print(chr(j + 65), end="")
print()
n = 7
left_triangle_pattern(n)
Output
A AB ABC ABCD ABCDE ABCDEF ABCDEFG
Explanation:
- Outer loop controls rows.
- Inner loop prints increasing letters from 'A' in each row.
2. Right Triangle Alphabet Pattern in Python
Creates a right-aligned triangle by adding spaces before printing increasing characters from 'A'.
def right_triangle_pattern(rows):
for i in range(rows):
for j in range(1, rows - i):
print(" ", end="")
for k in range(i + 1):
print(chr(65 + k), end="")
print()
n = 7
right_triangle_pattern(n)
Output
A
AB
ABC
ABCD
ABCDE
ABCDEF
ABCDEFG
Explanation:
- Adds leading spaces to align right.
- Characters from 'A' to the required length are printed.
3. Hollow Triangle Alphabet Pattern in Python
In this example, we will build a left-aligned triangle with only the boundary letters visible, leaving the inner space blank.
def hollow_left_triangle(rows):
for i in range(1, rows+1):
counter = 0
for j in range(i):
if j == 0 or j == i-1 or i == rows:
print(chr(65 + counter), end='')
counter += 1
else:
print(' ', end='')
print()
n = 7
hollow_left_triangle(n)
Output
A AB A B A B A B A B ABCDEFG
Explanation:
- Prints characters at the borders.
- Leaves spaces inside unless it's the last row.
4. Pyramid Alphabet Pattern in Python
Forms a centered pyramid where each row prints an odd number of letters starting from 'A'.
def pyramid_pattern(rows):
for i in range(rows):
print(' ' * (rows - i - 1), end='')
for k in range(2 * i + 1):
print(chr(65 + k), end='')
print()
n = 7
pyramid_pattern(n)
Output
A
ABC
ABCDE
ABCDEFG
ABCDEFGHI
ABCDEFGHIJK
ABCDEFGHIJKLM
Explanation:
- Each row is centered using spaces.
- Characters grow symmetrically from 'A'.
5. Upside Down Pyramid Alphabet Pattern in Python
Prints a reversed pyramid starting with the longest row and reducing characters each line.
def reverse_pyramid(rows):
for i in range(rows):
print(' ' * i, end='')
for j in range(2 * (rows - i) - 1):
print(chr(65 + j), end='')
print()
n = 7
reverse_pyramid(n)
Output
ABCDEFGHIJKLM
ABCDEFGHIJK
ABCDEFGHI
ABCDEFG
ABCDE
ABC
A
Explanation:
- Adds spaces to shift rows right.
- Characters reduce in odd numbers per row.
6. Hollow Pyramid Alphabet Pattern in Python
Creates a hollow pyramid by printing characters only on the border and full last row.
def hollow_pyramid(rows):
for i in range(rows):
print(' ' * (rows - i - 1), end='')
counter = 0
for k in range(2 * i + 1):
if k == 0 or k == 2 * i or i == rows - 1:
print(chr(65 + counter), end='')
counter += 1
else:
print(' ', end='')
print()
n = 7
hollow_pyramid(n)
Output
A
A B
A B
A B
A B
A B
ABCDEFGHIJKLM
Explanation:
- Borders of each row are filled.
- Middle spaces stay blank except in the last row.
7. Diamond Alphabet Pattern in Python
Combines an upright and reversed pyramid to form a symmetrical diamond of letters.
def print_diamond(rows, is_upright=True):
if is_upright:
for i in range(rows):
print(' ' * (rows - i - 1), end='')
for j in range(2 * i + 1):
print(chr(65 + j), end='')
print()
else:
for i in range(rows - 1):
print(' ' * (i + 1), end='')
for j in range(2 * (rows - i - 1) - 1):
print(chr(65 + j), end='')
print()
n = 7
print_diamond(n, True)
print_diamond(n, False)
Output
A
ABC
ABCDE
ABCDEFG
ABCDEFGHI
ABCDEFGHIJK
ABCDEFGHIJKLM
ABCDEFGHIJK
ABCDEFGHI
ABCDEFG
ABCDE
ABC
A
Explanation:
- First loop prints the upper triangle.
- Second loop prints the reversed lower part.
8. Hourglass Alphabet Pattern in Python
In this example, we will create an hourglass by stacking a reversed and upright pyramid pattern.
def print_hourglass(rows):
for i in range(rows - 1):
print(' ' * i, end='')
for k in range(2 * (rows - i) - 1):
print(chr(65 + k), end='')
print()
for i in range(rows):
print(' ' * (rows - i - 1), end='')
for k in range(2 * i + 1):
print(chr(65 + k), end='')
print()
n = 7
print_hourglass(n)
Output
ABCDEFGHIJKLM
ABCDEFGHIJK
ABCDEFGHI
ABCDEFG
ABCDE
ABC
A
ABC
ABCDE
ABCDEFG
ABCDEFGHI
ABCDEFGHIJK
ABCDEFGHIJKLM
Explanation:
- First half mirrors a reverse pyramid.
- Second half mirrors a pyramid.
9. Square Alphabet Pattern in Python
Prints a square where each row contains repeating uppercase letters in sequence.
def print_square_pattern(rows):
for i in range(rows):
for j in range(rows):
print(chr(65 + i), end=" ")
print()
n = 7
print_square_pattern(n)
Output
A A A A A A A B B B B B B B C C C C C C C D D D D D D D E E E E E E E F F F F F F F G G G G G G G
Explanation:
- Each row contains the same alphabet.
- Characters increment with rows.
10. Heart Alphabet Pattern in Python
Draws a heart shape by combining mirrored patterns using uppercase alphabets.
def print_heart_pattern(rows):
for i in range(rows // 2, rows, 2):
for j in range(1, rows - i, 2):
print(" ", end="")
for j in range(i):
print(chr(65 + j), end="")
for j in range(1, rows - i + 1):
print(" ", end="")
for j in range(i):
print(chr(65 + j), end="")
print()
for i in range(rows, 0, -1):
for j in range(i, rows):
print(" ", end="")
for j in range(i * 2):
print(chr(65 + j), end="")
print()
n = 10
print_heart_pattern(n)
Output:
ABCDE ABCDE
ABCDEFG ABCDEFG
ABCDEFGHI ABCDEFGHI
ABCDEFGHIJKLMNOPQRST
ABCDEFGHIJKLMNOPQR
ABCDEFGHIJKLMNOP
ABCDEFGHIJKLMN
ABCDEFGHIJKL
ABCDEFGHIJ
ABCDEFGH
ABCDEF
ABCD
AB
Explanation:
- Top part forms two arcs.
- Bottom descends forming a tapering shape.
11. Right Pascal Alphabet Pattern in Python
Forms a right Pascal triangle using alphabets with ascending and descending parts.
def right_pascal_triangle(rows):
for i in range(1, rows + 1):
print(" " * (rows - i), end="")
for j in range(1, i + 1):
print(chr(64 + j), end="")
print()
for i in range(rows - 1, 0, -1):
print(" " * (rows - i), end="")
for j in range(1, i + 1):
print(chr(64 + j), end="")
print()
n = 7
right_pascal_triangle(n)
Output
A
AB
ABC
ABCD
ABCDE
ABCDEF
ABCDEFG
ABCDEF
ABCDE
ABCD
ABC
AB
A
Explanation:
- First part grows rows.
- Second part reduces rows.
12. Left Pascal Alphabet Pattern in Python
Builds a left Pascal triangle with increasing and decreasing rows of characters.
def left_pascal(rows):
for i in range(1, rows + 1):
for j in range(1, i + 1):
print(chr(64 + j), end="")
print()
for i in range(rows - 1, 0, -1):
for j in range(1, i + 1):
print(chr(64 + j), end="")
print()
n = 7
left_pascal(n)
Output
A AB ABC ABCD ABCDE ABCDEF ABCDEFG ABCDEF ABCDE ABCD ABC AB A
Explanation:
- Upper half increases from 1 to n.
- Lower half decreases from n-1 to 1.
Related Article: Programs for Printing Pyramid