Letter O pattern
Write a Python program to print the letter ‘O’ with stars.
*** * * * * * * * * * * ***
Approach
- The top and bottom rows print stars in the middle columns.
- The other rows print a star only in the first and last columns.
Solution
rows, cols = 7, 5for r in range(rows): line = "" for c in range(cols): top_or_bottom = (r == 0 or r == rows - 1) and 0 < c < cols - 1 side = 0 < r < rows - 1 and (c == 0 or c == cols - 1) line += "*" if top_or_bottom or side else " " print(line)*** * * * * * * * * * * ***
Line by line
The first and last rows get stars only between the corners, which rounds the shape.
The middle rows get stars only on the left and right edges.
Each position adds a star or a space.
Learn the concept
New to this topic? Read the lesson, then come back.