By Using Loop, Print
-
*
-
*
-
*
-
*
Print hollow square by using loops in python.
CodePudding user response:
You may use below function to generate hollow square of side length "side"
def hollow_square(side):
for i in range(side):
for j in range(side):
if i == 0 or i == side-1 or j == 0 or j == side-1:
print("*", end="")
else:
print(" ", end="")
print()
hollow_square(5)
CodePudding user response:
you could do it in this way
n = int(input("enter the number:"))
def spaces(n): # creating spaces
sp = ""
for i in range(n-2):
sp = " "
return sp
for i in range(n):
if(i == 0) : # for top row
print('*'*n)
elif (i==n-1) : #for bottom row
print('*'*n)
else : # for the middle section
print(f"*{spaces(n)}*")