Patterns are a valuable exercise for enhancing logical thinking and coding skills. Using loops in Python, we can create diverse alphabetical patterns like stars, squares, pyramids, and triangles. In this discussion, we'll explore Python programs for printing these patterns using simple iteration and for loops. Before printing Python patterns, know ASCII: each alphabet corresponds to an ASCII value (e.g., A is 65). Uppercase (A-Z) values range from 65 to 90, and lowercase (a-z) from 97 to 122. Use `chr()` to print an alphabet via its ASCII value, like 'A' with ASCII 65.
What is an Alphabet Pattern?
An Alphabetic pattern is a visual arrangement of alphabets, whether uppercase or lowercase, presented in an aesthetically pleasing manner. These patterns can take on various predefined shapes such as squares, triangles, or even stars. Moreover, the customization of these patterns extends to choosing how they are filled.
Print Alphabet Pattern Programs in Python
Below are the Alphabet pattern programs in Python which we can print in Python.
Left Triangle Alphabet Pattern in Python
In this example, below Python code creates a function, `left_triangle_pattern`, to print a left-angled triangle of uppercase letters based on the specified number of rows. After defining the function, it sets the row count to 7 and calls the function to generate the desired pattern.
Python3
#define a function
def left_triangle_pattern(rows):
#goes to new lines
for i in range(rows):
#print characters in current line
for j in range(i + 1):
print(chr(j + 65), end="")
print()
#no of rows to span
n = 7
#call the function
left_triangle_pattern(n)
OutputA
AB
ABC
ABCD
ABCDE
ABCDEF
ABCDEFG
Space Complexity: O(1)
Time Complexity: O(n^2)
Right Triangle Alphabet Pattern in Python
In this example, below The Python code defines the function `right_triangle_pattern` to print a right-angled triangle of uppercase letters. It uses nested loops to manage line breaks, leading spaces, and character printing. The code sets the row count to 7 and calls the function to generate the pattern.
Python3
#define the function
def right_triangle_pattern(rows):
#changes lines
for i in range(rows):
#adds spaces
for j in range(1, rows - i):
print(" ", end="")
# prints characters
for k in range(i + 1):
print(chr(65 + k), end="")
print()
#rows to be spanned
n = 7
#call the function
right_triangle_pattern(n)
Output A
AB
ABC
ABCD
ABCDE
ABCDEF
ABCDEFG
Space Complexity: O(1)
Time Complexity: O(n^2)
Hollow Triangle Alphabet Pattern in Python
In this example, below given Python code defines a function `hollow_left_triangle` to print a hollow left-angled triangle pattern of uppercase letters. It uses nested loops, with the outer loop managing lines and the inner loop controlling character printing, including spaces for the hollow effect. The code sets the number of rows to 7 .
Python3
#define function
def hollow_left_triangle(rows):
#change rows
for i in range(1, rows+1):
counter = 0
for j in range(i):
# print characters at the end and start
if j == 0 or j == i-1:
print(chr(65 + counter), end='')
counter += 1
else:
# print spaces in between
if i != rows:
print(' ', end='')
# print characters in the last row
else:
print(chr(65 + counter), end='')
counter += 1
print()
#rows to be spanned
n = 7
#call the function
hollow_left_triangle(n)
OutputA
AB
A B
A B
A B
A B
ABCDEFG
Space Complexity: O(1)
Time Complexity: O(n^2)
Pyramid Alphabet Pattern in Python
In this example, below Python code defines a function `pyramid_pattern` to print a pyramid pattern of uppercase letters. It utilizes nested loops, with the first loop managing spaces before each line, and the second loop controlling the printing of an odd number of characters. The code then sets the number of rows to 7.
Python3
#define the function
def pyramid_pattern(rows):
for i in range(rows):
# prints spaces
for j in range(rows - i - 1):
print(' ', end='')
# prints odd number of characters
for k in range(2 * i + 1):
print(chr(65 + k), end='')
print()
#rows to be spanned
n = 7
#call the function
pyramid_pattern(n)
Output A
ABC
ABCDE
ABCDEFG
ABCDEFGHI
ABCDEFGHIJK
ABCDEFGHIJKLM
Space Complexity: O(1)
Time Complexity: O(n^2)
Upside Down Pyramid Alphabet Pattern in Python
In this example, below Python code defines a function `reverse_pyramid` to print a reversed pyramid pattern of uppercase letters. It uses nested loops, with the first loop managing spaces before each line, and the second loop controlling the printing of an odd number of characters. The code then sets the number of rows to 7 .
Python3
#define the function
def reverse_pyramid(rows):
for i in range(rows):
# prints spaces
for j in range(i):
print(' ', end='')
# prints an odd number of characters in each row
for j in range(2 * (rows - i) - 1):
print(chr(65 + j), end='')
print()
#rows to be spanned
n = 7
#call the function
reverse_pyramid(n)
OutputABCDEFGHIJKLM
ABCDEFGHIJK
ABCDEFGHI
ABCDEFG
ABCDE
ABC
A
Space Complexity: O(1)
Time Complexity: O(n^2)
Hollow Pyramid Alphabet Pattern in Python
In this example , below Python code defines a function `hollow_pyramid` to print a hollow pyramid pattern of uppercase letters. It uses nested loops, with the first loop managing spaces before each line, and the second loop controlling the printing of characters. The function includes conditions to print characters only at the start and end of each row, as well as in the last row for the hollow effect.
Python3
#define the function
def hollow_pyramid(rows):
for i in range(rows):
#print spaces
for j in range(rows - i - 1):
print(' ', end='')
#print characters
counter = 0
for k in range(2 * i + 1):
#print characters only in the start and end of row
if k == 0 or k == 2 * i:
print(chr(65 + counter), end='')
counter += 1
#print characters if it is the last row
else:
if i == rows - 1:
print(chr(65 + counter), end='')
counter += 1
else:
print(' ', end='')
print()
#rows to be spanned
n = 7
#call the function
hollow_pyramid(n)
Output A
A B
A B
A B
A B
A B
ABCDEFGHIJKLM
Space Complexity: O(1)
Time Complexity: O(n^2)
Diamond Alphabet Pattern in Python
In this example, below Python code defines a function `print_diamond` to print either an upright or a reversed diamond pattern of uppercase letters, based on the `is_upright` parameter. It uses nested loops, with the first loop managing spaces before each line, and the second loop controlling the printing of characters. The code sets the number of rows to 7 and calls the function twice.
Python3
#define the function
def print_diamond(rows, is_upright=True):
if is_upright:
for i in range(rows):
for j in range(rows - i - 1):
print(' ', end='')
for j in range(2 * i + 1):
print(chr(65 + j), end='')
print()
else:
for i in range(rows - 1):
for j in range(i + 1):
print(' ', end='')
for j in range(2 * (rows - i - 1) - 1):
print(chr(65 + j), end='')
print()
#rows to be spanned
n = 7
#call the function to print upright triangle
print_diamond(n, is_upright=True)
#call the function to print reverse triangle
print_diamond(n, is_upright=False)
Output A
ABC
ABCDE
ABCDEFG
ABCDEFGHI
ABCDEFGHIJK
ABCDEFGHIJKLM
ABCDEFGHIJK
ABCDEFGHI
ABCDEFG
ABCDE
ABC
A
Space Complexity: O(1)
Time Complexity: O(n^2)
Hourglass Alphabet Pattern in Python
In this example, below Python code defines a function `print_hourglass` to print an hourglass pattern of uppercase letters. It combines a reversed pyramid pattern and an upright pyramid pattern. The function uses nested loops to manage spaces and character printing for both the upper and lower halves of the hourglass.
Python3
#define function
def print_hourglass(rows):
#print reverse pyramid
for i in range(rows - 1):
for j in range(i):
print(' ', end='')
for k in range(2 * (rows - i) - 1):
print(chr(65 + k), end='')
print()
#print upright pyramid
for i in range(rows):
for j in range(rows - i - 1):
print(' ', end='')
for k in range(2 * i + 1):
print(chr(65 + k), end='')
print()
#rows to be spanned
n = 7
#call the function
print_hourglass(n)
OutputABCDEFGHIJKLM
ABCDEFGHIJK
ABCDEFGHI
ABCDEFG
ABCDE
ABC
A
ABC
ABCDE
ABCDEFG
ABCDEFGHI
ABCDEFGHIJK
ABCDEFGHIJKLM
Space Complexity: O(1)
Time Complexity: O(n^2)
Square Alphabet Pattern in Python
In this example , below Python code defines a function `print_square_pattern` to print a square pattern of uppercase letters. It uses nested loops, with the outer loop managing rows and the inner loop controlling the printing of characters.
Python3
#define the function
def print_square_pattern(rows):
#print rows
for i in range(rows):
#print characters in each row
for j in range(rows):
print(chr(65 + i), end=" ")
print()
#rows to be spanned
n = 7
#call the function
print_square_pattern(n)
OutputA 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
Space Complexity: O(1)
Time Complexity: O(n^2)
Heart Alphabet Pattern in Python
In this example, below Python code defines a function `print_heart_pattern` to print a heart-shaped pattern of uppercase letters. It consists of an upper part and a lower part. The upper part prints characters and spaces in a specific pattern, and the lower part prints characters in a descending order. The code sets the size of the heart based on the rows .
Python3
#define the function
def print_heart_pattern(rows):
#upper part of the heart
for i in range(rows // 2, rows, 2):
#prints spaces in the first half
for j in range(1, rows - i, 2):
print(" ", end="")
#prints alphabets in the first half
for j in range(i):
print(chr(65 + j), end="")
#prints spaces in the second half
for j in range(1, rows - i + 1, 1):
print(" ", end="")
#print alphabets in the second half
for j in range(i):
print(chr(65 + j), end="")
print()
#lower part of the heart
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()
#size of the heart based on the rows in the lower part
n = 10
#call the function
print_heart_pattern(n)
Output ABCDE ABCDE
ABCDEFG ABCDEFG
ABCDEFGHI ABCDEFGHI
ABCDEFGHIJKLMNOPQRST
ABCDEFGHIJKLMNOPQR
ABCDEFGHIJKLMNOP
ABCDEFGHIJKLMN
ABCDEFGHIJKL
ABCDEFGHIJ
ABCDEFGH
ABCDEF
...
Space Complexity: O(1)
Time Complexity: O(n^2)
Right Pascal Alphabet Pattern in Python
In this example, Below Python function `right_pascal_triangle` generates a right-angled Pascal's Triangle pattern using uppercase letters. It consists of ascending and descending parts, printing spaces and characters in a specific pattern. The code sets the row count to 7 and calls the function to produce the pattern.
Python3
#define the function
def right_pascal_triangle(rows):
#prints ascending part
for i in range(1, rows + 1):
#prints spaces
print(" " * (rows - i), end="")
#prints alphabets
for j in range(1, i + 1):
print(chr(64 + j), end="")
print()
#prints descending part
for i in range(rows - 1, 0, -1):
#prints spaces
print(" " * (rows - i), end="")
#prints alphabets
for j in range(1, i + 1):
print(chr(64 + j), end="")
print()
#rows to be spanned
n = 7
#call the function
right_pascal_triangle(n)
Output A
AB
ABC
ABCD
ABCDE
ABCDEF
ABCDEFG
ABCDEF
ABCDE
ABCD
ABC
AB
A
Space Complexity: O(1)
Time Complexity: O(n^2)
Left Pascal Alphabet Pattern in Python
In this example, below Python code defines a function `left_pascal` to print a left-angled Pascal's Triangle pattern using uppercase letters. It consists of an ascending part and a descending part, with each part printing characters in a specific pattern. The code sets the number of rows to 7 and calls the function to generate the left-angled Pascal's Triangle pattern.
Python3
#define the function
def left_pascal(rows):
#ascending part of the pattern
for i in range(1, rows + 1):
for j in range(1, i + 1):
print(chr(64 + j), end="")
print()
#descending part of the pattern
for i in range(rows - 1, 0, -1):
for j in range(1, i + 1):
print(chr(64 + j), end="")
print()
#rows to be spanend
n = 7
#call the function
left_pascal(n)
OutputA
AB
ABC
ABCD
ABCDE
ABCDEF
ABCDEFG
ABCDEF
ABCDE
ABCD
ABC
AB
A
Space Complexity: O(1)
Time Complexity: O(n^2)
Conclusion
In conclusion, exploring alphabet pattern programs in Python has provided a comprehensive insight into the versatility and creative potential of the language. From simple triangular patterns to intricate shapes, these programs serve as a practical and engaging way to enhance one's understanding of Python's loop structures and string manipulation capabilities. The ability to customize patterns by adjusting rows, columns, and repetition adds an extra layer of complexity, allowing learners to sharpen their programming skills.
Similar Reads
Python Program to Print Alphabetic Triangle Pattern
In this article, we examine a recursive method for printing character patterns in Python. Nesting loops are used in the conventional iterative approach to regulating character spacing and printing. We have provided the answer using both methods. This method prints an alphabet pattern in the form of
4 min read
Python List Creation Programs
Python provides multiple ways to create lists based on different requirements, such as generating lists of numbers, creating nested lists, forming lists of tuples or dictionaries, and more. This article covers various ways to create lists efficiently, including: Generating lists of numbers, strings,
2 min read
Python Program to Sort Words in Alphabetical Order
The task is to write a program that takes a list of words as input and sorts them in alphabetical order. The program should display the sorted list of words, ensuring that the sorting is case-insensitive. To sort words in alphabetical order in Python, we can use the sorted() function or the sort() m
2 min read
Print Alphabets till N-Python
Printing Alphabets till N in Python involves generating a sequence of uppercase or lowercase letters starting from 'A' (or 'a') up to the N-th letter of the alphabet. For example, if N = 5, the output should be: 'A B C D E'. Let's explore a few ways to implement this in a clean and Pythonic way. Usi
2 min read
Python program to generate a list of alphabets in lexical order
Prerequisite: chr() The following methods explain how a python list with alphabets in lexical(alphabetic) order can be generated dynamically using chr() method. Approach: The core of the concept in both methods is almost same, they only differ in implementation: Validate size of alphabet list(size=2
3 min read
Output of Python programs | Set 8
Prerequisite - Lists in Python Predict the output of the following Python programs. Program 1 [GFGTABS] Python list = [1, 2, 3, None, (1, 2, 3, 4, 5), ['Geeks', 'for', 'Geeks']] print len(list) [/GFGTABS]Output: 6Explanation: The beauty of python list datatype is that within
3 min read
Output of Python programs | Set 7
Prerequisite - Strings in Python Predict the output of the following Python programs. These question set will make you conversant with String Concepts in Python programming language. Program 1[GFGTABS] Python var1 = 'Hello Geeks!' var2 = "GeeksforGeeks" print "var1[0]: ",
3 min read
Python Coding Practice Problems
This collection of Python coding practice problems is designed to help you improve your overall programming skills in Python. The links below lead to different topic pages, each containing coding problems, and this page also includes links to quizzes. You need to log in first to write your code. You
1 min read
Python | First alphabet index
Sometimes, while working with Python strings we can have a problem in which we need to extract the first index of alpha character from string. This can have application in day-day programming. Lets discuss certain ways in which this task can be performed. Method #1: Using loop + regex The combinatio
6 min read
Python Programs
Practice with Python program examples is always a good choice to scale up your logical understanding and programming skills and this article will provide you with the best sets of Python code examples. The below Python section contains a wide collection of Python programming examples. These Python c
11 min read