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)

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.

Similar Reads

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....

Right Triangle Alphabet Pattern in Python

...

Hollow 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....

Pyramid Alphabet Pattern in Python

...

Upside Down Pyramid 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 ....

Hollow Pyramid Alphabet Pattern in Python

...

Diamond 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....

Hourglass Alphabet Pattern in Python

...

Square 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 ....

Heart Alphabet Pattern in Python

...

Right Pascal 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....

Left Pascal Alphabet Pattern in Python

...