Print Hollow Square

Print hollow square pattern.

Logic BuildingIntermediate
Logic Building
# Take n
n = int(input("Enter size: "))

# Print hollow square
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()

Output

Enter size: 5
*****
*   *
*   *
*   *
*****

Print stars only on borders.

Key Concepts:

  • First row, last row: all stars
  • First column, last column: stars
  • Middle: spaces