0
0
PythonProgramBeginner · 2 min read

Python Program to Print Alphabet Pattern

You can print an alphabet pattern in Python using nested for loops and the chr() function, for example: for i in range(5): print(' '.join(chr(65 + j) for j in range(i + 1))).
📋

Examples

Input5
OutputA A B A B C A B C D A B C D E
Input3
OutputA A B A B C
Input1
OutputA
🧠

How to Think About It

To print an alphabet pattern, think of rows where each row prints letters starting from 'A' up to a certain letter. Use a loop to control the number of rows, and inside it, use another loop to print letters from 'A' to the current row's letter. The chr() function helps convert numbers to letters.
📐

Algorithm

1
Get the number of rows as input.
2
For each row from 0 to number of rows - 1:
3
For each column from 0 to current row index:
4
Convert the column index to a letter starting from 'A' and print it.
5
Move to the next line after printing all letters in the row.
💻

Code

python
n = 5
for i in range(n):
    for j in range(i + 1):
        print(chr(65 + j), end=' ')
    print()
Output
A A B A B C A B C D A B C D E
🔍

Dry Run

Let's trace the example where n=3 through the code.

1

Start outer loop with i=0

Print letters from j=0 to 0: chr(65+0) = 'A'

2

Outer loop i=1

Print letters j=0 to 1: 'A' and 'B'

3

Outer loop i=2

Print letters j=0 to 2: 'A', 'B', 'C'

iPrinted Letters
0A
1A B
2A B C
💡

Why This Works

Step 1: Outer loop controls rows

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

Step 2: Inner loop prints letters

The inner for loop prints letters from 'A' up to the current row's letter using chr(65 + j).

Step 3: Print new line after each row

After printing letters in a row, print() moves to the next line for the next row.

🔄

Alternative Approaches

Using string join and list comprehension
python
n = 5
for i in range(n):
    print(' '.join(chr(65 + j) for j in range(i + 1)))
This method is more concise and uses string join for cleaner output.
Using ASCII values with while loops
python
n = 5
i = 0
while i < n:
    j = 0
    while j <= i:
        print(chr(65 + j), end=' ')
        j += 1
    print()
    i += 1
This uses while loops instead of for loops, which some beginners find easier to understand.

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

Time Complexity

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

Space Complexity

The program uses constant extra space, only variables for loop counters, so O(1) space.

Which Approach is Fastest?

Both for-loop and while-loop approaches have similar time complexity; using string join is cleaner but performance difference is negligible.

ApproachTimeSpaceBest For
Nested for loopsO(n^2)O(1)Simple and clear for beginners
String join with comprehensionO(n^2)O(1)Concise and readable code
While loopsO(n^2)O(1)Beginners preferring while loops
💡
Use chr(65 + index) to convert numbers to uppercase letters starting from 'A'.
⚠️
Beginners often forget to print a new line after each row, causing all letters to print on one line.