~2 min read • Updated Oct 11, 2025
Program Overview
This Python program uses nested loops to draw the uppercase letter 'M' using the '*' character in the terminal.
The number of rows is taken as input and determines the height of the letter.
This exercise helps practice conditional logic and nested loop structures for rendering complex text-based shapes.
Python Code:
def draw_letter_m(rows):
for i in range(rows):
for j in range(rows):
if j == 0 or j == rows - 1 or (i == j and j <= rows // 2) or (i + j == rows - 1 and j >= rows // 2):
print("*", end="")
else:
print(" ", end="")
print()
# Run the program
rows = int(input("Enter number of rows: "))
draw_letter_m(rows)
Sample Output (input: 7):
* *
** **
* * * *
* * *
* *
* *
* *
Step-by-Step Explanation:
- The outer loop iterates over rows, and the inner loop over columns
- The first and last columns always contain asterisks
- The left diagonal (top half) is drawn using i == j
- The right diagonal (top half) is drawn using i + j == rows - 1
- The result is a symmetrical 'M' shape made of asterisks
Written & researched by Dr. Shahin Siami