Function to Display a Rectangle - Python Programming Exercise

In this exercise, you will develop a Python program that defines a function to print a filled rectangle on the screen, where the dimensions (width and height) are provided as parameters.

 Category

Mastering Functions

 Exercise

Function To Display A Rectangle

 Objective

Develop a Python program that defines a function to print a filled rectangle on the screen, where the dimensions (width and height) are provided as parameters. For example, calling the function WriteRectangle(4, 3) will output:

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

Additionally, create a function WriteHollowRectangle that prints only the border of the rectangle. For example, WriteHollowRectangle(3, 4) will output:

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

 Example Python Exercise

 Copy Python Code
# Function to print a filled rectangle
def WriteRectangle(width, height):
    # Loop through the rows
    for i in range(height):
        # Print a row of '*' characters with the given width
        print('*' * width)

# Function to print a hollow rectangle
def WriteHollowRectangle(width, height):
    # Print the top border
    print('*' * width)

    # Print the middle rows with hollow spaces
    for i in range(height - 2):
        print('*' + ' ' * (width - 2) + '*')

    # Print the bottom border if height is greater than 1
    if height > 1:
        print('*' * width)

# Example usage
print("Filled Rectangle (4x3):")
WriteRectangle(4, 3)  # Print filled rectangle of width 4 and height 3

print("\nHollow Rectangle (3x4):")
WriteHollowRectangle(3, 4)  # Print hollow rectangle of width 3 and height 4

 Output

python rectangle.py
Filled Rectangle (4x3):
****
****
****

Hollow Rectangle (3x4):
****
*  *
*  *
****

 Share this Python Exercise

 More Python Programming Exercises of Mastering Functions

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.

  •  Iterative Palindrome Function

    In this exercise, you will develop a Python program with an iterative function to check if a string is a palindrome (symmetric). This exercise is perfect for p...

  •  Recursive Palindrome Function

    In this exercise, you will develop a Python program with a recursive function to determine if a string is symmetric (a palindrome). This exercise is perfect fo...

  •  Function FindMinMax

    In this exercise, you will develop a Python program with a function called "FindRange", where the user is prompted to input a minimum value (a number) and a maximum v...

  •  Function Multiply & MultiplyRecursive

    In this exercise, you will develop a Python program with two functions, Multiply and MultiplyRecursive, to compute the product of two numbers by using addition. The f...

  •  Functions: Hello and Goodbye

    In this exercise, you will develop a Python program where the main function should look like this: SayHello() and SayGoodbye(). You must define the functions SayHello...

  •  Parameterized Function

    In this exercise, you will develop a Python program where the main function should look like this: SayHello("John") and SayGoodbye(). You must define the functions Sa...