Founding Batch Offer ยท HLD ยท LLD ยท System Design at โ‚น799 until 30 Nov

Character Pattern

Build an alphabet triangle with chr and nested loops. Students trace character codes, row-local counting, and why letters continue past Z for large sizes.

IntermediatePhase 2 - Level 4: Pattern PrintingExample 6 of 13
character-pattern.py
1# Take n
2n = int(input("Enter number of rows: "))
3
4# Print pattern
5for i in range(1, n + 1):
6 for j in range(i):
7 print(chr(65 + j), end="") # 65 is ASCII of 'A'
8 print()

Output

Enter number of rows: 4
A
AB
ABC
ABCD

In this one

  • Convert codes with chr
  • Restart at A
  • Recognize alphabet limits

How it works

  1. 1

    Visit row widths one through n.

  2. 2

    Convert 65 + j for each zero-based column.

  3. 3

    Print characters together, then end the row.

Edge cases

What is the first printed character?

What is the first printed character?

A, from chr(65).

Does n = 27 wrap to A?

Does n = 27 wrap to A?

No. The code continues past Z.

What's going on

chr converts a character code into text. Starting at 65 produces A, and adding j advances the character on the current row.

Because j restarts at zero, each row starts with A. Three rows display A, AB, and ABC. The code does not wrap after Z; larger sizes continue into later Unicode code points rather than starting the alphabet again.

What is the first printed character? A, from chr(65).

Does n = 27 wrap to A? No. The code continues past Z.

Quiz

Tap an option. The answer stays hidden until you pick.

Question 1

What does j control?

Pick an answer to check it.

Question 2

What is the first printed character?

Pick an answer to check it.

Questions people ask

What is the first printed character?

A, from chr(65).

Does n = 27 wrap to A?

No. The code continues past Z.

What does j control?

The character position within a row.