This Python program explains a step-by-step process to print the alphabet pattern “A” using the Python range function. It includes a working sample for help.
Range() to Print Alphabet Pattern “A”
We have demonstrated multiple techniques to print the alphabet pattern “A” in this post. Please read and evaluate them one by one.
Alphabet pattern “A” program requirement
Our objective for this exercise is to produce an Alphabet “A” shape like the one given below. The pattern uses the star symbol and prints across nine lines.
Must Read: How to Print Patterns in Python – 20 Examples
You need to develop a program that takes a number and prints, using asterisks, a full “A” of the given length. For Example, if the line size is 7 or 11, the code should print:
# Test input: 7 ** * * * * **** * * * * * * # Test input: 11 ***** * * * * * * * * * * ******* * * * * * * * * * *
Also, you must make use of the Python range method for traversing through the loop.
Technique-1
Here, we’ve created a function and used inner loops with the range() function to print the desired “A” pattern.
""" Program Desc: Python 3.x program to print alphabet pattern "A """ """ Function to display alphabet pattern """ def print_alpha_A(size): # Main for loop for specified lines for m in range(size): # Child for loop for drawing the pattern for n in range((size // 2) + 1): # Now, printing two vertical lines if ((n == 0 or n == size // 2) and m != 0 or # Printing first vetical line to form A m == 0 and n != 0 and n != size // 2 or # Drawing the center line m == size // 2): print("*", end = "") else: print(" ", end = "") print() # Test Case-1 print_alpha_A(7) # Intentionally printing a blank line print() # Test Case-2 print_alpha_A(11)
Technique-2
In this technique, we’ll use the Python string property to repeat itself by a number specified along with the multiplication symbol. It is also interesting to know for you that we are using one loop to print the alphabet “A” pattern.
""" Program desc: This is a Python program to print A shape using Python string repeat feature in one loop """ """ Function to print alphabet A pattern in one loop """ def print_alphabet_pattern(lines): temp = 1 for iter in range(lines): if lines-iter-1 == lines//2: print((lines-iter) * ' ' + 1 * '*' + 2*(temp+iter) * '*' + 1 * '*') else: print((lines-iter) * ' ' + 1 * '*' + 2*(temp+iter) * ' ' + 1 * '*') # Test Case-1 print_alphabet_pattern(7) # Intentionally printing a blank line print() # Test Case-2 print_alphabet_pattern(11)
After executing the above code, you will see that it is producing the following “A” shape.
* * * * * * ********** * * * * * * * * * * * * * * * * ************** * * * * * * * * * *
You can now take clues from the above code samples and try some by yourself. It will make you think differently and help to build a logic of your own.