Articles

Program to print perimeter of a square using asterisks in Python

Program to print perimeter of a square using asterisks in Python


In this python programming article we are going to learn

  • program to print the perimeter of a square using asterisk in pyhon

Program to print the perimeter of a square using asterisk in Python

The program is as follows:

 

# Owner : TutorialsInhand Author : Devjeet Roy

size = int(input("Enter the no of rows: "))

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

The output of the program is as follows:

 

PS C:\Users\DEVJEET\Desktop\tutorialsInHand> python code.py
Enter the no of rows: 8
* * * * * * * *
*             *
*             *
*             *
*             *
*             *
*             *
* * * * * * * *
PS C:\Users\DEVJEET\Desktop\tutorialsInHand> python code.py
Enter the no of rows: 5
* * * * *
*       *
*       *
*       *
* * * * *

Few important tips about the program

1. First we ask the user to enter the number of rows of the pattern.

2. Then we use nested loops to print the pattern. The nested loops take care of the rows and columns respectively.

3. The asterisks are printed only on the first and last row and first and last column.

4. We use the blank print() statement to get the control to the next line.

 

program-to-print-perimeter-of-a-square-using-asterisks-in-python

 


Basic Python Programs

Would you like to see your article here on tutorialsinhand. Join Write4Us program by tutorialsinhand.com

About the Author
Devjeet Roy
Full Stack Web Developer & Data Science Enthusiast
Page Views :    Published Date : Oct 13,2022  
Please Share this page

Related Articles

Like every other website we use cookies. By using our site you acknowledge that you have read and understand our Cookie Policy, Privacy Policy, and our Terms of Service. Learn more Got it!