Hollow Square of Stars Printing Python Program

By | June 13, 2023

Hollow Square of Stars Printing Python Program – This program will print a hollow square of stars in Python programming language.

Print hollow square of stars in Python program

Print hollow square of stars in Python program

Here’s a Python program to print a hollow square:

n = 5
for i in range(n):
    if i == 0 or i == n - 1:
        print('*' * n)
    else:
        print('*' + ' ' * (n - 2) + '*')

In this program, we use a loop to iterate through each row of the square. We have a conditional if else statement to know whether the current row is the first or last row. If it is, we print a full line of asterisks (*). Otherwise, we print an asterisk (*) followed by spaces ( ) to create the hollow portion of the square and a star at end.

 

Loading

Leave a Reply

Your email address will not be published. Required fields are marked *