# Pattern Program In Python

In this article, you will learn about different kinds of patterns in Python. You will learn how to create and print pattern in Python using for loops and while loops. We will mainly focus on **star pattern** using python in this section.

## Star Pattern

Star pattern is a common pattern in many programming languages. It is a pattern that consists of a series of stars. The number of stars is determined by the number of rows and columns.

As a programmer, you will surely work on pattern programs. So, we will create a pattern program that prints a star pattern.

Here are some examples of star patterns:

The above image displays many different types of star patterns.

There are also other types of patterns that do not use start but numbers or letters. We will look at those in the next article.

Let's start with different pattern programs using python.

## List of Pattern Program In Python

The following is a list of pattern programs that we are going to look at in this article.

- Square Pattern
- Hollow Square Pattern
- Left triangle star Pattern
- Right triangle star Pattern
- Hollow triangle star Pattern
- Downward triangle star Pattern
- Pyramid Pattern
- Hollow Pyramid Pattern
- Reverse pyramid Pattern
- Diamond Pattern
- Hollow Diamond Pattern
- Hourglass Pattern
- Right Pascal star Pattern
- Left Pascal star Pattern
- Heart Pattern

## 1. Square Pattern in Python

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

The square pattern is the easiest pattern program. It is a pattern that has the shape of a square made of stars. Since it is a square, it is very easy to create and print. Let's see how to create and print a square pattern.

To create a square star pattern run 2 nested for loops. The outer loop will create the rows and the inner loop will create columns. The internal loop will print stars (*) and create a new line after every row.

The following is the code for creating a square pattern.

- Beginner
- Pro

```
# Square pattern program
# Create a list of rows
for i in range(0, 5):
# Create a list of columns
for j in range(0, 5):
print("*", end="")
print()
```

```
# Square pattern program
for i in range(0, 5):
# printing * for 5 times and a new line
print("*" * 5)
```

Output:

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

## 2. Hollow Square Pattern

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

The hollow square pattern is a bit more difficult pattern program than a simple square because here you will have to deal with spaces within the square.

To create a hollow square pattern, we will again run 2 nested for loops. The outer loop will run for a number of times as the size of the square. The inner loop will print only * in the first and last row and in other rows print * only at the first and last position and in the middle print spaces. The following is the code for creating a hollow square pattern.

- Beginner
- Pro

```
# hollow square pattern
size = 5
for i in range(size):
for j in range(size):
# print * completely in first and last row
# print * only in first and last position in other rows
if i == 0 or i == size - 1 or j == 0 or j == size - 1:
print('*', end='')
else:
print(' ', end='')
print()
```

```
# hollow square pattern
size = 5
for i in range(size):
# print star in first and last row
if i == 0 or i == size - 1:
print('*' * size)
else:
# print * in first and last position in other rows
print('*' + ' ' * (size - 2) + '*')
```

Output:

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

## 3. Left triangle star Pattern

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

The left triangle star pattern is a star pattern in the shape of a triangle. It is quite easy to create it.

To create a left triangle star pattern, run 2 nested loops where the internal loop will run for a number of times as the number of times external has run and print start.

- Beginner
- Pro

```
# Left triangle star pattern
n = 5
for i in range(1, n+1):
# internal loop run for i times
for k in range(1, i+1):
print("*", end="")
print()
```

```
# Left triangle star pattern
n = 5
for i in range(1, n+1):
print("*" * i)
```

Output:

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

## 4. Right triangle star Pattern

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

The right triangle star pattern is a star pattern in the shape of a triangle as shown above. It is similar to the left triangle star pattern but you will have to deal with spaces.

To create a right triangle star pattern there will be 2 nested loops, where external will run for a number of times as the size of the triangle and inside this, there will be 2 internal loops one will print * and the other will print spaces. The following is the code for creating a right triangle star pattern.

- Beginner
- Pro

```
# right triangle star pattern
size = 5
for i in range(size):
for j in range(1, size - i):
print(" ", end="")
for k in range(0, i + 1):
print("*", end="")
print()
```

```
# Left triangle star pattern
size = 5
for i in range(1, size+1):
print(" " * (size - i) + "*" * i)
```

Output:

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

## 5. Hollow triangle star Pattern

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

The hollow triangle star pattern is a star pattern in the shape of the triangle made of stars but hollow. You can see the pattern up there.

You can see in the above pattern that in every row except the first and last row, there will be stars only at the start and end of the row. And in the last row, there will be stars only at the start, end, or of the row.

```
# hollow triangle star pattern
n = 6
for i in range(1, n+1):
for j in range(i):
# print star only at start and end of the row
if j == 0 or j == i-1:
print('*', end='')
# print only star if it's last row
else:
if i != n:
print(' ', end='')
else:
print('*', end='')
print()
```

Output:

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

## 6. Downward triangle star Pattern

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

The downward triangle star pattern is the star pattern of a triangle upside down. It is very easy to create.

Run 2 nested loops where the internal loop will run for 'n' time, then 'n - 1' times till 0 and print star.

- Beginner
- Pro

```
# downward triangle star pattern
n = 5
for i in range(n):
# internal loop run for n - i times
for j in range(n - i):
print('*', end='')
print()
```

```
# downward triangle star pattern
n = 5
for i in range(n):
print('*' * (n - i))
```

Output:

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

## 7. Pyramid Pattern in python

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

The pyramid pattern is a very famous pattern in programming. It has the shape of an equilateral triangle and it is used to represent a pyramid. You can see the pattern up here.

The pyramid pattern has an odd number of stars in each row 1, 3, 5, 7, etc.

There will be 2 loops of which the first one will print spaces 'n - 1' in the first row, 'n - 2' in the second row, and so on and the second loop will print stars '2*i - 1' in the first row, '2*i - 2' in the second row and so on.

- Beginner
- Pro

```
# pyramid star pattern
n = 5
for i in range(n):
for j in range(n - i - 1):
print(' ', end='')
for k in range(2 * i + 1):
print('*', end='')
print()
```

```
# pyramid star pattern
n = 5
for i in range(n):
print(' ' * (n - i - 1) + '*' * (2 * i + 1))
```

Output:

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

## 8. Hollow pyramid pattern

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

The hollow pyramid pattern is a pyramid pattern made of stars but hollow. You can see the pattern up there.

You can see in the above pattern in every row there are 3 changes, first, there is a series of spaces, then a star and then another series of spaces, and finally a star. And in the last row, there are only stars.

So to create this pattern, first run a loop and print spaces. Then run another loop that prints a star then a series of spaces and finally print a star.

```
# hollow pyramid star pattern
n = 5
for i in range(n):
# printing spaces
for j in range(n - i - 1):
print(' ', end='')
# printing stars
for k in range(2 * i + 1):
# print star at start and end of the row
if k == 0 or k == 2 * i:
print('*', end='')
else:
if i == n - 1:
print('*', end='')
else:
print(' ', end='')
print()
```

Output:

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

## 9. Reverse pyramid pattern

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

The reverse pyramid pattern is the same as the pyramid pattern but it is upside down. See the pattern up there.

You can simply create it by running 2 nested loops where there will be 2 internal loops on to print spaces and another loop will print stars.

Let's see the full code here.

```
# reverse pyramid pattern
n = 5
for i in range(n):
# printing spaces
for j in range(i):
print(' ', end='')
# printing stars
for j in range(2*(n-i)-1):
print('*', end='')
print()
```

Output:

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

## 10. Diamond star pattern

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

The diamond star pattern is a star pattern with the shape of the diamond. You can see the pattern up here.

If you look closely, you will see that the pattern is a combination of a pyramid pattern and a downward triangle star pattern. So you can create this pattern by combining both patterns.

Here is the code to create this pattern.

```
# diamond star pattern
n = 5
# upward pyramid
for i in range(n):
for j in range(n - i - 1):
print(' ', end='')
for j in range(2 * i + 1):
print('*', end='')
print()
# downward pyramid
for i in range(n - 1):
for j in range(i + 1):
print(' ', end='')
for j in range(2*(n - i - 1) - 1):
print('*', end='')
print()
```

Output:

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

## 11. Hollow diamond pattern

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

The hollow diamond pattern is the same as the diamond star pattern but hollow. The pattern is up here.

This one is complex because you have to deal with multiple things like spaces, stars for each row where the pattern itself is divided into two parts upper pyramid and lower pyramid.

Let's see the code.

```
# hollow diamond star pattern
n = 5
# upward hollow pyramid
for i in range(n):
for j in range(n - i - 1):
print(' ', end='')
for j in range(2 * i + 1):
if j == 0 or j == 2 * i:
print('*', end='')
else:
print(' ', end='')
print()
# downward pyramid
for i in range(n - 1):
for j in range(i + 1):
print(' ', end='')
for j in range(2*(n - i - 1) - 1):
if j == 0 or j == 2*(n - i - 1) - 2:
print('*', end='')
else:
print(' ', end='')
print()
```

Output:

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

## 12. Hourglass pattern in python

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

The hourglass pattern is a pattern with the shape of an hourglass. When you observe the pattern, you will see that it is made up of two patterns. The first pattern is a downward pyramid pattern and the second pattern is an upward triangle pattern.

You can create this pattern by combining both patterns. The code is as follows.

```
# hourglass star pattern
n = 5
# downward pyramid
for i in range(n-1):
for j in range(i):
print(' ', end='')
for k in range(2*(n-i)-1):
print('*', end='')
print()
# upward pyramid
for i in range(n):
for j in range(n-i-1):
print(' ', end='')
for k in range(2*i+1):
print('*', end='')
print()
```

Output:

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

## 13. Right pascal triangle pattern

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

The right pascal triangle pattern is shown above. It can be clearly seen that it is made up of an upper triangle and a lower triangle.

So you can run 2 different loops one which creates the upper triangle and another which creates the lower triangle.

Here is the complete code.

- Beginner
- Pro

```
# right pascal triangle
n = 5
# upper triangle
for i in range(n):
for j in range(i + 1):
print('*', end="")
print()
# lower triangle
for i in range(n):
for j in range(n - i - 1):
print('*', end="")
print()
```

```
# right pascal triangle
n = 5
for i in range(n):
print('*' * (i + 1))
for i in range(n):
print('*' * (n - i - 1))
```

Output:

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

## 14. Left pascal triangle pattern

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

The left pascal triangle pattern is a mirror image of the right pascal triangle pattern. The pattern is shown above.

The left pascal triangle pattern is a little bit complicated than the right pascal triangle pattern because you will have to deal with both spaces and stars.

Let's see the code.

```
# left pascal triangle
n = 5
# upper triangle
for i in range(n):
# print spaces
for j in range(n - i - 1):
print(' ', end='')
# print stars
for k in range(i + 1):
print('*', end='')
print()
# lower triangle
for i in range(n - 1):
# print spaces
for j in range(i + 1):
print(' ', end='')
# print stars
for k in range(n - i - 1):
print('*', end='')
print()
```

Output:

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

## 15. Heart pattern in python

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

The heart pattern is a pattern with the shape of a heart. It is quite a complex pattern. But if you observe the code carefully then you will understand it easily.

```
# heart pattern
n = 6
# upper part of heart
for i in range(n//2, n, 2):
# print first spaces
for j in range(1, n-i ,2):
print(" ", end="")
# print first stars
for j in range(1, i+1, 1):
print("*", end="")
# print second spaces
for j in range(1, n-i+1, 1):
print(" ", end="")
# print second stars
for j in range(1, i+1, 1):
print("*", end="")
print()
# lower part
for i in range(n,0,-1):
for j in range(i, n, 1):
print(" ", end="")
for j in range(1, i*2, 1):
print("*", end="")
print()
```

Output:

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

## Conclusion

You have learned how to create patterns in python. You can now create many other different types of patterns.

We discussed 15 patterns with their codes and explanation.