Enhanced Rectangle V3 - Python Programming Exercise

In this exercise, you will develop a Python program that prompts the user for their name and a size, and displays a hollow rectangle with it. This exercise is perfect for practicing string manipulation and loops in Python. By prompting the user for their name and a size, and displaying a hollow rectangle with the name, you will gain hands-on experience in handling user input and output in Python. This exercise not only reinforces your understanding of string manipulation but also helps you develop efficient coding practices for managing user interactions.

 Category

Arrays, Lists, and Strings

 Exercise

Enhanced Rectangle V3

 Objective

Develop a Python program to prompt the user for their name and a size, and display a hollow rectangle with it:

Enter your name: Yo
Enter size: 4
YoYoYoYo
Yo Yo
Yo Yo
YoYoYoYo

(Note: The underscores _ should not be displayed on screen; your program should display blank spaces inside the rectangle)

 Example Python Exercise

 Copy Python Code
# Prompt the user for their name and the size of the rectangle
name = input("Enter your name: ")
size = int(input("Enter size: "))

# Print the top row of the rectangle
print(name * size)

# Print the middle rows with spaces inside
for i in range(size - 2):
    print(name + ' ' * (len(name) * (size - 2)) + name)

# Print the bottom row of the rectangle
if size > 1:
    print(name * size)

 Output

Enter your name: Yo
Enter size: 4
YoYoYoYo
Yo    Yo
Yo    Yo
YoYoYoYo

 Share this Python Exercise

 More Python Programming Exercises of Arrays, Lists, and Strings

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.

  •  Symmetrical Triangle

    In this exercise, you will develop a Python program that displays a centered triangle from a string entered by the user. This exercise is perfect for practicin...

  •  Urban Database

    In this exercise, you will develop a Python program to create a database for storing information about urban areas. In the first approach, you will store only the nam...

  •  Display Banner

    In this exercise, you will develop a Python program to mimic the basic Unix SysV "banner" utility, capable of displaying large texts. This exercise is perfect ...

  •  Right-Aligned Triangle

    In this exercise, you will develop a Python program that prompts the user for a string and displays a right-aligned triangle. This exercise is perfect for prac...

  •  Text Processing

    In this exercise, you will develop a Python program that prompts the user for a string and performs several transformations. The program will replace all lowercase 'a...

  •  Hierarchical Structures

    In this exercise, you will develop a Python program to store two pieces of data for a person: Name and Date of birth. The Date of birth must be a...