Open In App

Python Program to Print Alphabetic Triangle Pattern

Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we examine a recursive method for printing character patterns in Python. Nesting loops are used in the conventional iterative approach to regulating character spacing and printing. We have provided the answer using both methods. This method prints an alphabet pattern in the form of a pyramid. It uses nested loops to print the spaces and alphabets in each row after receiving the number of rows as input.

Print an Alphabetic Triangle Pattern using  Loops

The pattern generation algorithm involves the following steps. First, the input for the number of rows in the pattern is taken. Then, a loop is used to iterate from 1 to the input number of rows. Within this loop, spaces are printed using a nested loop that iterates from 1 to n-1, where n is the total number of rows and i is the current row number. Following that, another nested loop is used to print alphabets, starting from ‘A’ and incrementing by 1 for each iteration. This loop iterates from 1 to i, where i is the current row number. Lastly, a newline character is printed to move to the next row in the pattern. This algorithm helps generate a pattern with spaces and alphabets in a triangular shape, with each row having spaces on the left side and alphabets arranged in ascending order.

Python3




n = 10
 
for i in range(1, n+1):
    print(" "*(n-i), end="")
    for j in range(1, i+1):
        print(chr(64+j), end=" ")
    print()


Output:

        A 
A B
A B C
A B C D
A B C D E
A B C D E F
A B C D E F G
A B C D E F G H
A B C D E F G H I
A B C D E F G H I J

Time Complexity: The time complexity of this code is O(n*n), where ‘n’ is the number of rows in the pattern. The nested loops iterate ‘n’ times, making the time complexity quadratic.

Space Complexity: The space complexity of this code is also O(n*n). It stores the pattern in memory, which requires ‘n*n’ space. However, since the space used is only for storing the pattern temporarily, it can be considered constant space complexity.

Print Alphabetic Triangle Pattern using Recursion

The print_pattern function takes two parameters – n which represents the maximum value of I, and I which keeps track of the current value. The base case is when i become greater than n, in which case the recursion stops. The logic inside the function is similar to the original code but implemented using recursion. It prints the spaces and characters for each row, and then recursively calls itself with an incremented value of I until the base case is reached.

Python3




def print_pattern(n, i):
    if i <= n:
        print(" " * (n - i), end="")
        for j in range(1, i + 1):
            print(chr(64 + j), end=" ")
        print()
        print_pattern(n, i + 1)
 
 
n = 5
print_pattern(n, 1)


Output:

    A 
A B
A B C
A B C D
A B C D E

Python Program to Print Alphabetic Triangle Pattern using list comprehension

To print an alphabetic triangle pattern using list comprehension in Python, we can create a list of lists, where each sublist contains the alphabetic characters for each row. We will then use list comprehension to generate the triangle pattern. Here’s the Python program to achieve this:

Python




def generate_alphabetic_triangle(rows):
    # Generate the list of lists containing
    # alphabetic characters for each row
    alphabets = [[chr(65 + j) for j in range(i)] for i in range(1, rows + 1)]
    return alphabets
 
 
def print_alphabetic_triangle(alphabets):
    # Print the alphabetic triangle
    # pattern using list comprehension
    for row in alphabets:
        print(' '.join(row))
 
 
if __name__ == "__main__":
    num_rows = 5  # Adjust the number of rows as needed
 
    alphabets_triangle = generate_alphabetic_triangle(num_rows)
    print_alphabetic_triangle(alphabets_triangle)


Output:

A
A B
A B C
A B C D
A B C D E


Last Updated : 28 Aug, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads