Python program to print a star hollow square pattern

Python program to print a hollow square pattern of star:

In this tutorial, we will learn how to print a hollow square pattern in Python. I will show you how to print that pattern using star characters (*), but you can also modify the program to print the pattern using any other character or symbol like &,%,$,#,@, etc.

To print the pattern, we can either use for loops or while loops. I will show you how to write the code in both ways. The program will print similar to the following pattern:

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

We will ask the user to enter the size of one side of the square. Since all sides are equal, we will write the program to read one side and print the hollow square using the user-provided size. The size of the above pattern is 6.

Method 1: Print a hollow square using for loops:

The following program uses two for loops to print a hollow square pattern:

length = int(input("Enter the size of the square: "))

for i in range(length):
    for j in range(length):
        if(i == 0 or i == length - 1 or j == 0 or j == length - 1):
            print('*', end = ' ')
        else:
            print(' ', end = ' ')
    print()

Download it on Github

  • It takes the size of the square as input from the user and assigns this value to the length variable.
  • It uses two for loops. The outer loop runs from i = 0 to i = length - 1.
  • For each value of the variable i or for each iteration of the outer loop, the inner loop will run from j = 0 to j = length - 1.
  • Inside the inner loop, print a * if the value of i or j is equal to 0 or length - 1. This will print the character only for the first, last rows and columns. Otherwise it prints a blank space.
  • At the end of each iteration of the inner loop, we need to print a new line character to move to the next line.

Sample output :

Enter the size of the square  : 6
* * * * * *
*         *
*         *
*         *
*         *
* * * * * *

python print hollow square

How it worked:

To learn how the program worked, let me change the print statement of the else statement to print another character:

length = int(input("Enter the size of the square: "))

for i in range(length):
    for j in range(length):
        if(i == 0 or i == length - 1 or j == 0 or j == length - 1):
            print('*', end = ' ')
        else:
            print('$', end = ' ')
    print()

Now, if you run the program, it will print the above square as:

Enter the size of the square  : 6
* * * * * *
* $ $ $ $ *
* $ $ $ $ *
* $ $ $ $ *
* $ $ $ $ *
* * * * * *

As you can see, the second print statement was originally used to print the blank spaces, but in this example, we have changed the print statement to print the $ symbol.

  • The outer print statement is used to print the * and the inner print statement is used to print the blank spaces.
  • The outer loop will run for a length number of time. In this example, it runs for i = 0 to i = 6. The inner loop will also run for the same time, i.e. from j = 0 to j = 6. The outer loop will point to the rows and the inner loop will point to the columns of the square.
  • We are printing the * only if i == 0, i == length - 1, j == 0 or j == length - 1. i.e. we are printing the symbol for the first row, last row, first column and last column. For other values, we are printing blank spaces.

I hope that you have got an understanding of how the program works and how it printed out the pattern. Now, let’s try to implement this using a while loop:

Method 2: Print a hollow square using while loops:

Similar to the previous example, we can also implement it using while loops. The following program uses while loops to print the hollow square pattern:

length = int(input("Enter the side of the square: "))

row = 1

while(row <= length):
    column = 1;
    while(column <= length ):
        if(row == 1 or row == length or column == 1 or column == length):
            print('*', end = ' ')
        else:
            print(' ', end = ' ')
        column = column + 1
    row = row + 1
    print()

Download it on Github

Sample Output :

It will print similar patterns:

Enter the side of the square: 6
* * * * * *
*         *
*         *
*         *
*         *
* * * * * *

python print hollow square

How to read the character as input from the user:

Let’s take the character as input from the user and use that character to print the pattern. The user will enter the character that we will use to print the square:

length = int(input("Enter the side of the square: "))
ch = input("Enter the character to use in the square: ")

for row in range(length):
    for column in range(length):
        if(row == 0 or row == length - 1 or column == 0 or column == length - 1):
            print(ch, end = ' ')
        else:
            print(' ', end = ' ')
    print()
  • It reads the character and assigns it to the ch variable. The program prints the ch variable instead of *.

Output:

If you run the above program, it will print output as below:

@ @ @ @ @ @ @
@           @
@           @
@           @
@           @
@           @
@ @ @ @ @ @ @

python print hollow square

Similar tutorials: