Part of the series

Several example codes

~2 min read • Updated Oct 8, 2025

Program Overview

This Python program reads the number of rows from the user and prints a hollow rectangle pattern using asterisks (*).
The rectangle has a solid border and a hollow center. The number of rows determines the height of the rectangle, and the width is fixed or can be adjusted.


Python Code:


def draw_hollow_rectangle(rows, cols):
    for i in range(rows):
        for j in range(cols):
            if i == 0 or i == rows - 1 or j == 0 or j == cols - 1:
                print("*", end="")
            else:
                print(" ", end="")
        print()

# Run the program
rows = int(input("Enter number of rows: "))
cols = rows  # You can change this to any fixed width if needed
draw_hollow_rectangle(rows, cols)

Sample Output (rows = 5):


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

Step-by-Step Explanation:

- The outer loop runs for each row
- The inner loop runs for each column
- Asterisks are printed at the borders: first/last row or first/last column
- Spaces are printed inside the rectangle to create the hollow effect


Written & researched by Dr. Shahin Siami