# How to draw Square shape in Python

Hello Techies,

The Square shape in python is the most frequently asked question in most interview processes. In this blog, we will learn how to draw a square shape in Python.

## Square shape in Python

### #1. Square shape in Python using Star Pattern

To draw this pattern we are using loop and range function of Python. Basically in this program we are specifically using 5 rows and 5 columns. You can modify it as per your requirement.

```for row in range(5):
for col in range(5):
print("*", end=" ")
print()```

After executing the above code, the following results will be obtained –

```* * * * *
* * * * *
* * * * *
* * * * *
* * * * *```

### #2. Square shape in Python using Star Pattern with user input

We are using Python’s loop and range function to draw this pattern. Basically in this program, we are taking input from the user to create a square shape.

```input_number = int(input("Enter the side of square:"))

for row in range(input_number):
for col in range(5):
print("*", end=" ")
print()```

After executing the above code, the following results will be obtained –

```Enter the side of square:5
* * * * *
* * * * *
* * * * *
* * * * *
* * * * *```

### #3. Python number square shape

We are using Python’s loop and range function to draw this pattern. Basically in this program, we are writing the program for the number square.

```for row in range(5):
for col in range(5):
print(row, end=" ")
print()```

After executing the above code, the following results will be obtained –

```0 0 0 0 0
1 1 1 1 1
2 2 2 2 2
3 3 3 3 3
4 4 4 4 4```

### #4. The hollow square pattern in Python

We are using Python’s loop and range function to draw this pattern. Basically in this program, we are writing the program for hollow square.

```for i in range(5):
for j in range(5):
if(i == 0 or i == 5 - 1 or j == 0 or j == 5 - 1):
print('*', end = '  ')
else:
print(' ', end = '  ')
print()```

After executing the above code, the following results will be obtained –

```*  *  *  *  *
*           *
*           *
*           *
*  *  *  *  *```

### #5. Numbers in square shape in Python

Method 1: Using for loop

```val = int(input("Enter the number of rows:"))
n_list = [[0 for x in range(val)] for y in range(val)]
n = 1
low = 0
high = val - 1
count = int((val + 1) / 2)
for i in range(count):
for j in range(low, high + 1):
n_list[i][j] = n
n = n + 1
for j in range(low + 1, high + 1):
n_list[j][high] = n
n = n + 1
for j in range(high - 1, low - 1, -1):
n_list[high][j] = n
n = n + 1
for j in range(high - 1, low, -1):
n_list[j][low] = n
n = n + 1
low = low + 1
high = high - 1

for i in range(val):
for j in range(val):
print(n_list[i][j], end="\t")
print()```

After executing the above code, the following results will be obtained –

```1       2       3       4       5
16      17      18      19      6
15      24      25      20      7
14      23      22      21      8
13      12      11      10      9```

### #6. Square in python using turtle library

In this program, we are using the turtle library to draw the square shape.

```import turtle

t = turtle.Turtle()

# drawing 1st side
t.forward(100)
t.left(90)

# drawing 2nd side
t.forward(100)
t.left(90)

# drawing 3rd side
t.forward(100)
t.left(90)

# drawing 4th side
t.forward(100)
t.left(90)

turtle.done()```

After executing the above code, the following results will be obtained –