Part of the series

Several example codes

~1 min read • Updated Oct 11, 2025

Program Overview

This Python program reads an integer from the user, representing the number of lines.
In each line, it prints a combination of characters to represent the letter 'Z' using asterisks ('*').
This exercise helps practice loop structures and creative character-based output formatting.


Python Code:


def print_z_shape(lines):
    for i in range(lines):
        if i == 0 or i == lines - 1:
            print("*" * lines)
        else:
            print(" " * (lines - i - 1) + "*")

# Run the program
lines = int(input("Enter number of lines: "))
print_z_shape(lines)

Sample Output (input: 5):


*****
   *
  *
 *
*****

Step-by-Step Explanation:

- The program first reads the number of lines from the user
- The first and last lines are filled with asterisks to form the top and bottom of the 'Z'
- The middle lines contain a single asterisk, shifted leftward to form the diagonal
- The result is a stylized 'Z' shape made entirely of '*' characters


Written & researched by Dr. Shahin Siami