Shorts: Python Program for Hollow Square Star Pattern Printing

Posted by

Python Program to Print Hollow Square Star Pattern

Python Program to Print Hollow Square Star Pattern

In this tutorial, we will write a Python program to print a hollow square star pattern using nested loops.

Here is the Python code:


def print_hollow_square(n):
    for i in range(n):
        for j in range(n):
            if i == 0 or i == n-1 or j == 0 or j == n-1:
                print("*", end="")
            else:
                print(" ", end="")
        print()

# Taking input from the user
size = int(input("Enter the size of the square: "))
print_hollow_square(size)

Copy and paste this code into a Python interpreter or a file, and run it to see the hollow square star pattern.

Feel free to modify the code to create different patterns or shapes with stars. Have fun exploring and experimenting with Python!

That’s it for this tutorial on the Python program to print a hollow square star pattern. If you found this helpful, please like, share, and subscribe for more programming tutorials.