Tutorials Tonight
Follow Us

Pattern Program in Python


In this article, you will look at 30 different pattern program in Python.

Star Pattern In Python

Star pattern is a common pattern program created in any programming language. It is a pattern that consists of a series of stars that create some sort of shape.

In the image below you can see some of the star patterns.

star pattern example

There are also other types of patterns that do not use stars but numbers or alphabets. We will also look at these in brief here.

Let's start with different pattern programs using python.

pattern program in python

List of Pattern Program In Python

We are going to see the following pattern program in python here in this article.

  1. Square Pattern in Python
  2. Hollow Square Pattern in Python
  3. Left triangle star Pattern in Python
  4. Right triangle star Pattern in Python
  5. Left downward triangle pattern
  6. Right downward triangle pattern
  7. Hollow triangle star Pattern
  8. Pyramid Pattern in Python
  9. Hollow Pyramid Pattern in Python
  10. Reverse pyramid Pattern in Python
  11. Diamond star pattern in Python
  12. Hollow diamond star pattern in Python
  13. Hourglass star pattern in python
  14. Right Pascal star Pattern in Python
  15. Left Pascal star Pattern in python
  16. Heart Pattern in Python
  17. Plus star pattern
  18. Cross star pattern
  19. Left triangle number pattern
  20. Right triangle number pattern
  21. Number pyramid pattern
  22. Number pyramid reverse pattern
  23. Hollow number pyramid pattern
  24. Number diamond pattern
  25. Hollow number diamond pattern
  26. Alphabet pyramid pattern
  27. Reverse alphabet pyramid pattern
  28. Hollow alphabet pyramid pattern
  29. Alphabet diamond pattern
  30. Hollow alphabet diamond 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.

Let's see how to create and print a square pattern.

  1. Take size of the square as input from the user or just set a value.
  2. Run a nested loop for the number of rows and columns.
  3. Print the star in each iteration and print a new line after each row.
  • Pro
# Square pattern program
size = 5

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

for i in range(0, size):
    # printing * for 'size' times and a new line
    print("*" * size)

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.

Here are the steps to create a hollow square pattern.

  1. To create hollow pattern again run a nested for loop.
  2. External loop will be same as the previous square pattern but inside internal loop you will have to check the condition.
  3. If its the first or last row or column then print onlu stars.
  4. Otherwise check if its the first or last column then print star else print spaces.
  • 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 In Python

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

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

Steps to create a left triangle star pattern:

  1. Run a nested for loop where internal loop will run for number of times external loop has run.
  2. Print star in each iteration of internal loop.
  3. Print a new line at the end of internal loop.
  • 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 In Python

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

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.

Steps to create a right triangle star pattern:

  1. Create a loop that will run for the number of rows (size).
  2. Inside this we will have 2 loops, first will print spaces and second will print stars. (look at pattern above)
  3. Spaces will be printed for size - i times and stars will be printed for i times. Where i is the current row.
  4. Print new line at the end of both internal loops.
  • 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. Left Downward Triangle Pattern

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

The left downward triangle 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 size - i times and external loop will run size times. Here i is the current row.

  • 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:

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

6. Right Downward Triangle Pattern

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

The right downward triangle pattern is a pattern that is upside down and has perpendicular to the right side.

Steps to create a right downward triangle pattern:

  1. From the above given pattern you can see that, this also requires 2 internal loop.
  2. First internal loop print spaces for i times and second internal loop print stars for size - i times.
  3. Here i is index of the current row.
  4. Print new line everytime after the internal loop.
# downward triangle star pattern
size = 5
for i in range(size):
    for j in range(i):
        print(" ", end="")
    for j in range(size, i, -1):
        print("*", end="")
    print()

Output:

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

7. Hollow triangle star Pattern

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

The hollow triangle star pattern is a star pattern in the shape of the triangle made of stars but hollow.

Follow these steps:

  1. You can see the pattern up there be sure to understand it. Stars are printed only in first last column of any row, except the last row.
  2. Run a nested loop where external loop runs for the size of triangle.
  3. Inside create internal loop. Inside it check if its first or last row then print only stars. If not print starts only at first and last column else print spaces.
# 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:

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

8. 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.

  1. Again we need to nest the loops.
  2. Create 2 internal loop, first print spaces and second print stars.
  3. Print spaces for number of times equal to size - i and stars for 2 * i - 1 times.
  4. Here i is the current row.
  • Pro
# pyramid star pattern
n = 5
for i in range(1, n+1):
    for j in range(n - i):
        print(' ', end='')
    for k in range(2 * i - 1):
        print('*', end='')
    print()
# pyramid star pattern
n = 5
for i in range(1, n+1):
    print(' ' * (n - i) + '*' * (2 * i - 1))

Output:

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

9. Hollow Pyramid Pattern In Python

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

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

Follow these steps to create hollow pyramid pattern:

  1. You can observe that we have to handle spaces at 2 different places. First before printing star and second between the pyramid.
  2. Create nested loop with 2 internal loops. First loop just create space, second loop print both spaces & stars with some logics.
  3. The first internal loop print spaces for size - i times.
  4. The second internal loop pexecutes for 2 * i - 1 times and checks if it is first or last row then print star, if not check if it is first or last column then print star else print space.
# hollow pyramid star pattern
n = 5
for i in range(1, n+1):
    # printing spaces
    for j in range(n - i):
        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 - 2:
            print('*', end='')
        else:
            if i == n:
                print('*', end='')
            else:
                print(' ', end='')
    print()

Output:

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

10. Reverse Pyramid Pattern In Python

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

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

Just like the pyramid pattern, the reverse pyramid pattern follows the same logic. The only difference is that we have to print the spaces and stars with reverse logic.

# reverse pyramid pattern
n = 5

for i in range(1, n+1):
    # printing spaces
    for j in range(i-1):
        print(' ', end='')
    # printing stars
    for j in range(2*(n-i)+1):
        print('*', end='')
    print()

Output:

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

Stay Ahead, Learn More.

11. Diamond Star Pattern In Python

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

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:

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

12. Hollow Diamond Star Pattern In Python

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

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:

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

13. Hourglass Star 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:

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

14. Right Pascal Star Pattern In Python

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

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.

  • 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:

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

15. Left Pascal Star Pattern In Python

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

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 more 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:

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

16. 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 the 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:

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

17. Plus pattern program in Python

The plus pattern is a pattern with the shape of a plus sign (+).

  *
  *
*****
  *
  *

The complete code is given below.

# plus pattern in python

size = 5

for i in range(size):
    for j in range(size):
        if i == size // 2:
            print('*', end='')
        else:
            if j == size // 2:
                print('*', end='')
            else:
                print(' ', end='')
    print()

Output:

  *  
  *  
*****
  *  
  *

18. Cross pattern program in Python

The cross pattern is a pattern with the shape of a cross sign (X).

*   *
 * * 
  *  
 * * 
*   *

Here is the complete code to create the cross pattern.

# cross pattern in python
size = 5

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

Output:

*   *
 * * 
  *  
 * * 
*   *

19. Left Number Triangle Pattern Program

The left number triangle pattern is a triangle pattern that is made of numbers and has perpendicular on its left side.

1
12
123
1234
12345

The complete code for the left number triangle pattern is given below.

# left number triangle pattern
size = 5
for i in range(size):
    for j in range(i+1):
        print(j+1, end="")
    print()

Output:

1
12
123
1234
12345

20. Right Number Triangle Pattern Program

The right number triangle pattern is a triangle pattern that is made of numbers and has perpendicular on its right side.

    1
   12
  123
 1234
12345

The complete code for the right number triangle pattern is given below.

# right number triangle pattern
size = 5
for i in range(size):
    # print spaces
    for j in range(1, size - i):
        print(" ", end="")
    # print numbers
    for k in range(i + 1):
        print(k + 1, end="")
    print()

Output:

    1
   12
  123
 1234
12345

21. Number Pyramid Pattern Program In Python

The number pyramid pattern is a pattern that is made of numbers and has a pyramid shape.

    1
   123
  12345
 1234567
123456789

The complete code for the number pyramid pattern is given below.

# number pyramid pattern
size = 5
for i in range(size):
    # print spaces
    for j in range(size - i - 1):
        print(" ", end="")
    # print numbers
    for k in range(2 * i + 1):
        print(k+1, end="")
    print()

Output:

    1
   123
  12345
 1234567
123456789

22. Reverse Number Pyramid Pattern Program In Python

The reverse number pyramid pattern is a number pyramid reversed 180 degrees.

123456789
 1234567
  12345
   123
    1

The complete code for the reverse number pyramid pattern is given below.

# reverse number pyramid pattern
size = 5
for i in range(size):
    # print spaces
    for j in range(i):
        print(" ", end="")
    # print numbers
    for k in range(2 * (size - i) - 1):
        print(k+1, end="")
    print()

Output:

123456789
 1234567
  12345
   123
    1

23. Hollow Number Pyramid Pattern Program

The hollow number pyramid pattern is a number pyramid pattern that has a hollow space in the middle.

    1
   1 2
  1   2
 1     2
123456789

The complete code for the hollow number pyramid pattern is given below.

# hollow number pyramid pattern
size = 5
for i in range(size):
    # print spaces
    for j in range(size - i - 1):
        print(" ", end="")

    # print numbers
    count = 1;
    for k in range(2 * i + 1):
        if i == 0 or i == size - 1:
            print(count, end="")
            count += 1
        else:
            if k == 0 or k == 2 * i:
                print(count, end="")
                count += 1
            else:
                print(" ", end="")
    print()

Output:

    1
   1 2
  1   2
 1     2
123456789

24. Number Diamond Pattern Program

The number diamond pattern is a diamond pattern that is made of numbers.

    1
   123
  12345
 1234567
123456789
 1234567
  12345
   123
    1

The complete code for the number diamond pattern is given below.

# number diamond pattern
size = 5
num = 1

# upside pyramid
for i in range(1, size + 1):
    # printing spaces
    for j in range(size, i - 1, -1):
        print(" ", end="")
    # printing star
    for k in range(0, i * 2 - 1):
        print(num, end="")
        num += 1
    # set the number to 1
    num = 1
    print()
# downside pyramid
for i in range(1, size):
    # printing spaces
    for j in range(0, i+1):
        print(" ", end="")
    # printing star
    for k in range((size - i) * 2 - 1):
        print(num, end="")
        num += 1
    # set num to 1
    num = 1
    print()

Output:

    1
   123
  12345
 1234567
123456789
 1234567
  12345
   123
    1

25. Hollow Number Diamond Pattern Program

The hollow number diamond pattern is a diamond pattern that is made of numbers and is hollow inside.

    1
   1 2
  1   2
 1     2
1       2
 1     2
  1   2
   1 2
    1

The complete code for the hollow number diamond pattern is given below.

# hollow diamond number pattern
size = 5
num = 1

# upward hollow pyramid
for i in range(size):
    for j in range(size - i - 1):
        print(' ', end='')
    for j in range(2 * i + 1):
        if j == 0 or j == 2 * i:
            print(num, end='')
            num += 1
        else:
            print(' ', end='')
    # set num to 1
    num = 1
    print()

# downward pyramid
for i in range(size - 1):
    for j in range(i + 1):
        print(' ', end='')
    for j in range(2*(size - i - 1) - 1):
        if j == 0 or j == 2*(size - i - 1) - 2:
            print(num, end='')
            num += 1
        else:
            print(' ', end='')
    # set num to 1
    num = 1
    print()

Output:

    1
   1 2
  1   2
 1     2
1       2
 1     2
  1   2
   1 2
    1

Let's now create some pattern programs using alphabets instead of stars or numbers.

26. Alphabet Pyramid Pattern Program

The alphabet pyramid pattern is a pyramid pattern that is made of alphabets.

    A
   ABC
  ABCDE
 ABCDEFG
ABCDEFGHI

The complete code for the alphabet pyramid pattern is given below.

# alphabet pyramid pattern
size = 5
alpha = 65

for i in range(size):
    # print spaces
    for j in range(size - i):
        print(" ", end="")
    # print alphabets
    for k in range(2 * i + 1):
        print(chr(alpha + k), end="")
    print()

Output:

    A
   ABC
  ABCDE
 ABCDEFG
ABCDEFGHI

27. Reverse Alphabet Pyramid Pattern Program

The reverse alphabet pyramid pattern is a pyramid pattern that is made of alphabets and is upside down.

ABCDEFGHI
 ABCDEFG
  ABCDE
   ABC
    A

The complete code for the reverse alphabet pyramid pattern is given below.

# reverse alphabet pyramid pattern
size = 5
alpha = 65

for i in range(size):
    # print spaces
    for j in range(i):
        print(" ", end="")
    # print alphabets
    for k in range(2 * (size - i) - 1):
        print(chr(alpha + k), end="")
    print()

Output:

ABCDEFGHI
 ABCDEFG
  ABCDE
   ABC
    A

28. Hollow Alphabet Pyramid Pattern

The hollow alphabet pyramid pattern is a pyramid pattern that is made of alphabets and is hollow inside.

    A
   B C
  D   E
 F     G
HIJKLMNOP

The complete code for the hollow alphabet pyramid pattern is given below.

# hollow alphabet pyramid pattern
size = 5
alpha = 65
num = 0

for i in range(size):
    for j in range(size - i - 1):
        print(" ", end="")
    for k in range(2 * i + 1):
        if i == 0 or i == size - 1:
            print(chr(alpha + num), end="")
            num += 1
        else:
            if k == 0 or k == 2 * i:
                print(chr(alpha + num), end="")
                num += 1
            else:
                print(" ", end="")
    print()

Output:

    A
   B C
  D   E
 F     G
HIJKLMNOP

29. Alphabet Diamond Pattern Program

The alphabet diamond pattern is a diamond pattern that is made of alphabets.

    A
   ABC
  ABCDE
 ABCDEFG
ABCDEFGHI
 ABCDEFG
  ABCDE
   ABC
    A

The complete code for the alphabet diamond pattern is given below.

# alphabet diamond pattern
size = 5
alpha = 65
num = 0

# upside pyramid
for i in range(1, size + 1):
    # printing spaces
    for j in range(size, i - 1, -1):
        print(" ", end="")
    # printing alphabets
    for k in range(0, i * 2 - 1):
        print(chr(alpha + num), end="")
        num += 1
    num = 0
    print()
#downward pyramid
for i in range(1, size):
    # printing spaces
    for j in range(0, i+1):
        print(" ", end="")
    # printing alphabets
    for k in range((size - i) * 2 - 1):
        print(chr(alpha + num), end="")
        num += 1
    num = 0
    print()

Output:

    A
   ABC
  ABCDE
 ABCDEFG
ABCDEFGHI
 ABCDEFG
  ABCDE
   ABC
    A

30. Hollow Alphabet Diamond Pattern

The hollow alphabet diamond pattern is a diamond pattern that is made of alphabets and is hollow inside.

    A
   A B
  A   B
 A     B
A       B
 A     B
  A   B
   A B
    A

The complete code for the hollow alphabet diamond pattern is given below.

# hollow alphabet diamond pattern
# hollow diamond alphabet pattern
size = 5
alpha = 65
num = 0

# upward hollow pyramid
for i in range(size):
    for j in range(size - i - 1):
        print(' ', end='')
    for j in range(2 * i + 1):
        if j == 0 or j == 2 * i:
            print(chr(alpha+num), end='')
            num += 1
        else:
            print(' ', end='')
    # set num to 0
    num = 0
    print()

# downward pyramid
for i in range(size - 1):
    for j in range(i + 1):
        print(' ', end='')
    for j in range(2*(size - i - 1) - 1):
        if j == 0 or j == 2*(size - i - 1) - 2:
            print(chr(alpha+num), end='')
            num += 1
        else:
            print(' ', end='')
    # set num to 0
    num = 0
    print()

Output:

    A
   A B
  A   B
 A     B
A       B
 A     B
  A   B
   A B
    A

Conclusion

After looking at 30 pattern program in python you have learned the basics of creating patterns. Now you can create your own patterns and make them look beautiful. Try the alphabet pattern program discussed in the next article.