Print Hollow Square

Print hollow square pattern.

IntermediatePhase 2 - Level 4: Pattern PrintingExample 7 of 13
print-hollow-square.py
1# Take n
2n = int(input("Enter size: "))
3
4# Print hollow square
5for i in range(n):
6 for j in range(n):
7 if i == 0 or i == n-1 or j == 0 or j == n-1:
8 print("*", end="")
9 else:
10 print(" ", end="")
11 print()

Output

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

What's going on

Print stars only on borders.

Key Concepts:

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