Instagram
youtube
Facebook
Twitter

Python Program to Print Hollow Rectangle

A Python Program to Print Hollow Rectangle?


Code Explanation:
● Variables rows and cols define size of the rectangle.

● Loop runs from 0 to 2 (3 times) for rows.

● First and last rows print all stars using "*" * cols.

● Middle row prints star, spaces, then star.

● This creates a rectangle with a hollow center.

 

Program:

rows = 3

cols = 5

for i in range(rows):

    if i == 0 or i == rows - 1:

        print("*" * cols)

    else:

        print("*" + " " * (cols - 2) + "*")