Objective
Develop a Python program that prompts the user for a symbol and a width, and displays a hollow square of that width using that symbol for the outer border, as shown in this example:
Enter a symbol: 4
Enter the desired width: 3
444
4 4
444
Example Python Exercise
Show 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: "))
# Use a while loop to display the hollow square
i = 0
while i < width:
if i == 0 or i == width - 1:
print(symbol * width)
else:
print(symbol + " " * (width - 2) + symbol)
i += 1
Output
Enter a symbol: 4
Enter the desired width: 3
444
4 4
444
Share this Python Exercise