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 ) |
A ABC ABCDE ABCDEFG ABCDEFGHI ABCDEFGHIJK ABCDEFGHIJKLM ABCDEFGHIJK ABCDEFGHI ABCDEFG ABCDE ABC A
Space Complexity: O(1)
Time Complexity: O(n^2)
Alphabet Pattern Programs in Python
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.
Contact Us