0
0
PythonProgramBeginner · 2 min read

Python Program to Print Alphabet Triangle Pattern

You can print an alphabet triangle pattern in Python using nested loops like for i in range(1, n+1): print(''.join(chr(64+j) for j in range(1, i+1))) where n is the number of rows.
📋

Examples

Input3
OutputA AB ABC
Input5
OutputA AB ABC ABCD ABCDE
Input1
OutputA
🧠

How to Think About It

To print an alphabet triangle, think of each row as printing letters starting from 'A' up to the row number. For example, row 3 prints 'A', 'B', and 'C'. Use a loop to go through each row and inside it, another loop to print letters from 'A' to the current row's letter.
📐

Algorithm

1
Get the number of rows (n) as input
2
For each row from 1 to n:
3
Initialize an empty string for the current row
4
For each position from 1 to the current row number:
5
Convert the position to the corresponding uppercase letter starting from 'A'
6
Add the letter to the current row string
7
Print the current row string
💻

Code

python
n = int(input('Enter number of rows: '))
for i in range(1, n + 1):
    row = ''.join(chr(64 + j) for j in range(1, i + 1))
    print(row)
Output
Enter number of rows: 5 A AB ABC ABCD ABCDE
🔍

Dry Run

Let's trace input 3 through the code

1

Input number of rows

n = 3

2

First row (i=1)

Generate letters from 1 to 1: chr(64+1) = 'A'; print 'A'

3

Second row (i=2)

Generate letters from 1 to 2: chr(64+1)='A', chr(64+2)='B'; print 'AB'

4

Third row (i=3)

Generate letters from 1 to 3: 'A', 'B', 'C'; print 'ABC'

Row (i)Letters GeneratedPrinted Output
1AA
2A BAB
3A B CABC
💡

Why This Works

Step 1: Loop through rows

The outer loop runs from 1 to n, controlling how many rows to print.

Step 2: Generate letters for each row

The inner part uses chr(64 + j) to convert numbers to letters starting from 'A' (ASCII 65).

Step 3: Print the row

Joining letters into a string and printing creates the triangle shape line by line.

🔄

Alternative Approaches

Using ASCII values with nested loops
python
n = int(input('Enter number of rows: '))
for i in range(n):
    for j in range(i + 1):
        print(chr(65 + j), end='')
    print()
This method uses nested loops with print and end='' to print letters without joining strings.
Using string slicing
python
import string
n = int(input('Enter number of rows: '))
alphabets = string.ascii_uppercase
for i in range(1, n + 1):
    print(alphabets[:i])
This method uses Python's built-in string of alphabets and slices it for each row.

Complexity: O(n^2) time, O(n) space

Time Complexity

The program uses nested loops: the outer loop runs n times and the inner loop runs up to n times, resulting in O(n^2) time.

Space Complexity

The space used is mainly for the string of letters in each row, which grows up to length n, so O(n) space.

Which Approach is Fastest?

All approaches have similar time complexity; using string slicing is more readable but uses extra memory for the alphabet string.

ApproachTimeSpaceBest For
Using chr() with joinO(n^2)O(n)Simple and clear for beginners
Nested loops with printO(n^2)O(1)Memory efficient, prints directly
String slicingO(n^2)O(n)Readable and uses built-in strings
💡
Use chr(65 + index) to convert numbers to uppercase letters starting from 'A'.
⚠️
Beginners often forget that ASCII 'A' starts at 65, so using 64 + index is needed to get correct letters.