Letter A pattern
Write a Python program to print the letter ‘A’ with stars.
* * * * * * * * * * * * * * * * * * * *
Approach
- Row 0 is the rounded top, the middle row is the cross bar, and the other rows have stars on both sides.
Solution
rows, cols = 7, 6for r in range(rows): line = "" for c in range(cols): top = r == 0 and 0 < c < cols - 1 bar = r == rows // 2 sides = r > 0 and c in (0, cols - 1) line += "* " if top or bar or sides else " " print(line)* * * * * * * * * * * * * * * * * * * *
Line by line
The top row has stars only between the corners.
The middle row (index 3) is the full cross bar.
Every other row has stars at the two edges.
Learn the concept
New to this topic? Read the lesson, then come back.