Python Practice › Loop Programs › Program 43

Letter A pattern

MediumLoop Programs · 43 of 90

Write a Python program to print the letter ‘A’ with stars.

Advertisement
Output
  * * * *   
*         * 
*         * 
* * * * * * 
*         * 
*         * 
*         * 

Approach

  1. Row 0 is the rounded top, the middle row is the cross bar, and the other rows have stars on both sides.

Solution

Python
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)
Output
  * * * *   
*         * 
*         * 
* * * * * * 
*         * 
*         * 
*         * 

Line by line

Line 5

The top row has stars only between the corners.

Line 6

The middle row (index 3) is the full cross bar.

Line 7

Every other row has stars at the two edges.

Learn the concept

New to this topic? Read the lesson, then come back.

More loop programs

← All 90 Loop Programs

Leave a Comment