Empty Square - Python Programming Exercise

This Python program prompts the user to input a symbol and a width, then displays a hollow square with the specified width. The square's outer border is created using the provided symbol, while the inner part of the square remains empty, creating a hollow effect. This program is an excellent exercise for practicing nested loops in Python, as the outer loop handles the rows while the inner loop manages the columns. The hollow square structure is a great way to demonstrate how to work with user inputs and conditional statements to create dynamic patterns. Learning how to create shapes and patterns like this is an important skill in Python programming, especially for those interested in game development, graphics programming, or even data visualization. By using a simple symbol and adjusting the width, this program provides an easy introduction to handling loops and conditions in Python, while simultaneously building the user’s understanding of how to manipulate character output effectively.

 Category

Mastering Flow Control

 Exercise

Empty Square

 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

 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: "))

# 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

 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.

  •  Output

    This Python program prompts the user for two integer numbers and calculates their product without using the "*" operator. Instead, it uses consecutiv...

  •  Absolute Magnitude

    This Python program calculates and displays the absolute value of a number x. The absolute value of a number is defined as the number itself if it is positive,...

  •  Empty Rectangle

    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...

  •  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...