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) |
ABCDE ABCDE ABCDEFG ABCDEFG ABCDEFGHI ABCDEFGHI ABCDEFGHIJKLMNOPQRST ABCDEFGHIJKLMNOPQR ABCDEFGHIJKLMNOP ABCDEFGHIJKLMN ABCDEFGHIJKL ABCDEFGHIJ ABCDEFGH ABCDEF ...
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.