Empty Rectangle - Python Programming Exercise

This Python program prompts the user for a symbol, width, and height, and then displays a hollow rectangle using that symbol for the outer border. The program generates the rectangle by printing the symbol along the outer edges, while leaving spaces inside to create the hollow effect. For instance, if the user enters the symbol "4", a width of 3, and a height of 5, the program will display a rectangle with a border of 4's and spaces in the middle. This exercise is useful for practicing nested loops in Python, allowing beginners to understand how to iterate over rows and columns to manipulate how text is displayed on the screen. The outer border of the rectangle is made by printing the symbol on the first and last rows, as well as the first and last columns of each row. In between these borders, spaces are used to create the hollow interior. This structure is achieved using nested loops, where the outer loop controls the number of rows and the inner loop controls the number of columns in each row. By implementing this program, learners can practice looping structures and conditional logic to determine when to print a symbol and when to print a space. This also reinforces the importance of user input for dynamic display output in Python programs.

 Category

Mastering Flow Control

 Exercise

Empty Rectangle

 Objective

Develop a Python program that prompts the user for a symbol, a width, and a height, and displays a hollow rectangle of that width and height using that symbol for the outer border, as in this example:

Enter a symbol: 4
Enter the desired width: 3
Enter the desired height: 5

444
4 4
4 4
4 4
444

 Example Python Exercise

 Copy Python Code
# Prompt the user to enter a symbol
symbol = input("Enter a symbol: ")

# Prompt the user to enter the desired width
width = int(input("Enter the desired width: "))

# Prompt the user to enter the desired height
height = int(input("Enter the desired height: "))

# Use a while loop to display the hollow rectangle
i = 0
while i < height:
    if i == 0 or i == height - 1:
        print(symbol * width)
    else:
        print(symbol + " " * (width - 2) + symbol)
    i += 1

 Output

Enter a symbol: 4
Enter the desired width: 3
Enter the desired height: 5
444
4 4
4 4
4 4
444

 Share this Python Exercise

 More Python Programming Exercises of Mastering Flow Control

Explore our set of Python Programming Exercises! Specifically designed for beginners, these exercises will help you develop a solid understanding of the basics of Python. From variables and data types to control structures and simple functions, each exercise is crafted to challenge you incrementally as you build confidence in coding in Python.

  •  Data Analysis

    This Python program calculates various basic statistical operations such as sum, average, minimum, and maximum based on user input. The pr...

  •  Ternary Operator, Positive & Lesser

    This Python program prompts the user to input two numbers and then uses the conditional operator (?) to perform a series of checks on the input. The program first che...

  •  Prime Digit

    This Python program prompts the user to input an integer and determines whether it is a prime number or not. A prime number is defined as a number great...

  •  Dispense Change

    This Python program calculates the change for a purchase, using the largest possible coins or bills. The program prompts the user for the price o...

  •  Error Handling

    This Python program prompts the user for two numbers and performs the division operation. It uses a try..except block to catch potential errors such as ...

  •  Positive and Negative Numbers

    In this exercise, you will develop a Python program that prompts the user to input a number and then determines if the number is positive or n...