Alphabet Pattern Programs In Python
From the last article, we know about pattern programs and star pattern programs. Now in this article, we are going to see alphabet pattern programs in python with code and explanations.
Alphabet Pattern
An alphabet pattern is a pattern made up of alphabets (A-Z or a-z). The pattern made from the alphabet can be geometrical shapes like square, triangle, pyramid, diamond, etc, or non-geometrical shapes like heart, star, etc.
Let us see some examples of alphabet patterns in python.
Apart from the patterns shown in the above image, there can be other countless alphabet patterns programs. All you need is a little imagination.
Print A to Z in Python using for loop
Before we go further in creating the patterns first let's see how to loop through the alphabet in python.
As you know every character has an ASCII value. For example, A has an ASCII value of 65 and Z has an ASCII value of 90.
We are going to use these values and loop from 65 to 90, then convert the number value to the character and print it. To convert the ASCII value to the character we can use the chr() function.
Let us see an example in action.
# loop through alphabets and print them
# 65 in char is A
# 90 in char is Z
for i in range(65, 91):
print(chr(i), end=" ")
Output:
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
Now we know how to loop through the alphabet in python. Let's now create patterns.
1. Square Alphabet Pattern in Python
Alphabet patterns with the same shape can have different types of character filling. Like for square pattern we can have every next character changing, character changing only in a row, character changing only in a column, etc. See the image below.
Let us create all of the above square patterns one by one.
# Square pattern 1
A B C D E F G H I J K L M N O P Q R S T U V W X Y
In the above pattern, we have 5 rows and 5 columns and the character is changing every next time.
To create this simply create 2 nested for loops where the outer loop repeats a row and the internal loop prints the character in a column.
To change the character in every iteration you can set a counter and increment it by 1 every time in the inner loop.
# square alphabet pattern
size = 5
count = 0
for i in range(size):
for j in range(size):
print(chr(65 + count), end=" ")
# changing charater
count += 1
print()
Output:
A B C D E F G H I J K L M N O P Q R S T U V W X Y
# Square pattern 2
A A A A A B B B B B C C C C C D D D D D E E E E E
This pattern is the same as the pattern discussed above but the character is not changing every next time but changing only in a new row.
To achieve this you can use the iterator value of external loop (i) and add it to 65 and convert it to the character. Since the iterator value of the external loop changes only in a new row, so we can use it here.
# square alphabet pattern
size = 5
for i in range(size):
for j in range(size):
print(chr(65 + i), end=" ")
print()
Output:
A A A A A B B B B B C C C C C D D D D D E E E E E
# Square pattern 3
A B C D E A B C D E A B C D E A B C D E A B C D E
You can see in this pattern character is changing throughout the row but reset to A after every row.
This can be achieved by using the iterator value of the internal loop and adding it to 65 and convert it to the character.
# square alphabet pattern
size = 5
for i in range(size):
for j in range(65, 65+size):
print(chr(j), end=' ')
print()
Output:
A B C D E A B C D E A B C D E A B C D E A B C D E
2. Left Triangle Alphabet Pattern in Python
A AB ABC ABCD ABCDE
The left triangle pattern is a pattern in the shape of a triangle created using alphabets.
To create this pattern we need to use nested for loop and print the character in a row.
Here is the complete code.
# Left triangle pattern
n = 5
for i in range(n):
for j in range(i+1):
print(chr(j + 65), end="")
print()
Output:
A AB ABC ABCD ABCDE
3. Right triangle Pattern
A AB ABC ABCD ABCDE
You can see above how the right triangle alphabet pattern looks like.
You can see there is a space at the beginning of every row. So we will have to deal with spaces too.
Create 2 nested loops where the external loop will run an internal loop for the size of the pattern. There will be 2 internal loops first one will print spaces and the other will print the character.
# right triangle pattern
size = 5
for i in range(size):
for j in range(1, size - i):
print(" ", end="")
for k in range(i + 1):
print(chr(65 + k), end="")
print()
Output:
A AB ABC ABCD ABCDE
4. Hollow triangle alphabet Pattern
A AB A B A B A B ABCDEF
The hollow triangle pattern is a bit complex to create because of the spaces in the pattern.
To create this you can create 2 nested loops where the internal loop will check if it is the first and the last position of the row then print character else print spaces and if it is the last row then print only characters.
The complete code for this is given below.
# hollow triangle alphabet pattern
n = 6
for i in range(1, n+1):
count = 0
for j in range(i):
# print alphabets only at start and end of the row
if j == 0 or j == i-1:
print(chr(65 + count), end='')
count += 1
# print only alphabets if it's last row
else:
if i != n:
print(' ', end='')
else:
print(chr(65 + count), end='')
count += 1
print()
Output:
A AB A B A B A B ABCDEF
5. Pyramid Alphabet Pattern in Python
A ABC ABCDE ABCDEFG ABCDEFGHI
The pyramid pattern is quite a famous pattern you will see this even in programming challenges.
You can see the pattern above has an odd number of alphabets in each row 1, 3, 5, 7, etc.
There will be 2 loops where the first loop will print spaces and the second loop will print the 2n + 1 alphabets.
# pyramid alphabet pattern
n = 5
for i in range(n):
for j in range(n - i - 1):
print(' ', end='')
for k in range(2 * i + 1):
print(chr(65 + k), end='')
print()
Output:
A ABC ABCDE ABCDEFG ABCDEFGHI
6. Hollow pyramid pattern
A A B A B A B ABCDEFGHI
The hollow pyramid pattern is a little bit tricky to create.
See the code below the first internal loop prints spaces and the second loop checks if it is the first or last position of the row then prints the character and if its the last row then prints only characters.
# hollow pyramid alphabets pattern
n = 5
for i in range(n):
# printing spaces
for j in range(n - i - 1):
print(' ', end='')
# printing alphabets
count = 0
for k in range(2 * i + 1):
# print alphabets at start and end of the row
if k == 0 or k == 2 * i:
print(chr(65 + count), end='')
count += 1
else:
if i == n - 1:
print(chr(65 + count), end='')
count += 1
else:
print(' ', end='')
print()
Output:
A A B A B A B ABCDEFGHI
7. Reverse pyramid pattern
ABCDEFGHI ABCDEFG ABCDE ABC A
The reverse pyramid pattern is equivalent to a pyramid pattern but upside down. See the pattern up there.
This is very simple to create see the complete code below.
# reverse pyramid pattern
n = 5
for i in range(n):
# printing spaces
for j in range(i):
print(' ', end='')
# printing alphabet
for j in range(2*(n-i)-1):
print(chr(65 + j), end='')
print()
Output:
ABCDEFGHI ABCDEFG ABCDE ABC A
8. Diamond pattern
A ABC ABCDE ABCDEFG ABCDEFGHI ABCDEFG ABCDE ABC A
The diamond pattern when observed carefully you will see is made up of 2 parts, the first part is the same as the pyramid pattern and the second part is the same as the reverse pyramid pattern.
So to create this you can run 2 sets of loops that print the upward and downward parts of the pattern.
Here is the complete code to create this pattern.
# diamond alphabet pattern
n = 5
# upward pyramid
for i in range(n):
for j in range(n - i - 1):
print(' ', end='')
for j in range(2 * i + 1):
print(chr(65 + j), end='')
print()
# downward pyramid
for i in range(n - 1):
for j in range(i + 1):
print(' ', end='')
for j in range(2*(n - i - 1) - 1):
print(chr(65 + j), end='')
print()
Output:
A ABC ABCDE ABCDEFG ABCDEFGHI ABCDEFG ABCDE ABC A
9. Hourglass pattern in python
ABCDEFGHI ABCDEFG ABCDE ABC A ABC ABCDE ABCDEFG ABCDEFGHI
The hourglass pattern is the shape of an hourglass. You can replicate the pattern by cutting the diamond pattern in half and then mirroring it.
So code is quite similar with just a few tricky changes.
# hourglass alphabet pattern
n = 5
# downward pyramid
for i in range(n-1):
for j in range(i):
print(' ', end='')
for k in range(2*(n-i)-1):
print(chr(65 + k), end='')
print()
# uppward pyramid
for i in range(n):
for j in range(n-i-1):
print(' ', end='')
for k in range(2*i+1):
print(chr(65 + k), end='')
print()
Output:
ABCDEFGHI ABCDEFG ABCDE ABC A ABC ABCDE ABCDEFG ABCDEFGHI
10. Right pascal triangle pattern
A AB ABC ABCD ABCDE ABCD ABC AB A
You can see the right pascal triangle pattern shown here. Studying all the above patterns you can recognize the structure pattern and how to create it.
Here is the complete code for this pattern.
# right pascal triangle
n = 5
# upper triangle
for i in range(n):
for j in range(i + 1):
print(chr(65 + j), end="")
print()
# lower triangle
for i in range(n):
for j in range(n - i - 1):
print(chr(65 + j), end="")
print()
Output:
A AB ABC ABCD ABCDE ABCD ABC AB A
11. Heart pattern in python
ABC ABC ABCDE ABCDE ABCDEFGHIJKL ABCDEFGHIJ ABCDEFGH ABCDEF ABCD AB
The heart pattern can be created using alphabets and spaces. It is a bit complex to create this pattern.
You can see the complete code of heart pattern below.
# heart pattern
n = 6
# upper part of heart
for i in range(n//2, n, 2):
# print first spaces
for j in range(1, n-i, 2):
print(" ", end="")
# print first alphabet
for j in range(i):
print(chr(65 + j), end="")
# print second spaces
for j in range(1, n-i+1, 1):
print(" ", end="")
# print second alphabet
for j in range(i):
print(chr(65 + j), end="")
print()
# lower part
for i in range(n, 0, -1):
for j in range(i, n):
print(" ", end="")
for j in range(i*2):
print(chr(65 + j), end="")
print()
Output:
ABC ABC ABCDE ABCDE ABCDEFGHIJKL ABCDEFGHIJ ABCDEFGH ABCDEF ABCD AB
Conclusion
You have learned to create many different types of alphabet patterns in python. Based on the experience now you can create your own patterns.
If you want to learn more about creating patterns you can see pattern programs in python.