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.
1# Take n2n = int(input("Enter number of rows: "))34# Print pattern5for 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
Visit row widths one through n.
- 2
Convert 65 + j for each zero-based column.
- 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.
๐Related Content
- ๐
Phase 2 - Learn Concepts
Review Phase 2 concepts and explanations
- ๐
Phase 2 - Quiz
Test your Phase 2 understanding with quiz questions
- ๐ป
Phase 2 - All Practice Problems
Explore all practice problems for Phase 2
- ๐
Master Your Logic Building - Complete Course
Browse all phases and tutorials
- ๐ง
Logic Building Overview
Learn about the complete logic building curriculum