Skip to main content

Programs to print different Square, rectangle in Python Language

Below program will accept width and breadth from user. We can print SOLID rectangle, need to use two for loops along with print function with end parameter.

Step 1) Accept breadth and width from user
Step 2) First for loop is to print

#Python program to print hollow rectangle, solid rectangle.
#accept width and breadth from user
width=int(input("Enter Width\t"))
breadth=int(input("Enter breadth \t"))
for i in range(0,breadth):
    for j in range(0,width):
        print("*",end="")
    print()

OUTPUT

Enter Width	5
Enter breadth 	3

*****
*****
*****

Python Program to print hollow rectangle

This tricky program. It is important to use print function along with end parameter effectively. Here is the complete code

#Python program to print hollow rectangle
#accept width and breadth from user
width=int(input("Enter Width\t"))
breadth=int(input("Enter breadth \t"))
for i in range(0,breadth):
    for j in range(0,width):
        if (i==0 or j ==0 or i==breadth or j==width):
             print(" *",end="")
        else:
             print(" ",end="*")
    print()

Output

Enter Width	3
Enter breadth 	4
 * * *
 * * *
 * * *
 * * *

Use free online python compiler to write execute any python program.

Leave a Reply

Your email address will not be published.