Cara menggunakan python star pattern programs

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.

Cara menggunakan python star pattern programs

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

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

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.

  • Beginner
  • 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.

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

  • 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()

# 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()
0

Output:

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


Cara menggunakan python star pattern programs
report this ad

4. Right Triangle Star Pattern In Python

# 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()
2

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.

  • Beginner
  • 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()
3

# 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()
4

Output:

# 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()
2


5. Left Downward Triangle Pattern

# 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()
6

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.

  • Beginner
  • 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()
7

# 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()
8

Output:

# 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()
6


6. Right Downward Triangle Pattern

# Square pattern program
size = 5

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

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.
# Square pattern program
size = 5

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

Output:

# Square pattern program
size = 5

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


7. Hollow triangle star Pattern

# Square pattern program
size = 5

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

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.
# Square pattern program
size = 5

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

Output:

# Square pattern program
size = 5

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


8. Pyramid Pattern in python

# Square pattern program
size = 5

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

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.

  • Beginner
  • Pro

# Square pattern program
size = 5

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

# Square pattern program
size = 5

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

Output:

# Square pattern program
size = 5

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


9. Hollow Pyramid Pattern In Python

*****
*****
*****
*****
*****
0

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.
*****
*****
*****
*****
*****
1

Output:

*****
*****
*****
*****
*****
0


10. Reverse Pyramid Pattern In Python

*****
*****
*****
*****
*****
3

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.

*****
*****
*****
*****
*****
4

Output:

*****
*****
*****
*****
*****
3


Stay Ahead, Learn More

  • Pyramid Pattern In Python
  • Number Pattern Programs In Python
  • Alphabet Pattern Programs In Python

11. Diamond Star Pattern In Python

*****
*****
*****
*****
*****
6

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.

*****
*****
*****
*****
*****
7

Output:

*****
*****
*****
*****
*****
6


12. Hollow Diamond Star Pattern In Python

*****
*****
*****
*****
*****
9

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.

*****
*   *
*   *
*   *
*****
0

Output:

*****
*****
*****
*****
*****
9


13. Hourglass Star Pattern In Python

*****
*   *
*   *
*   *
*****
2

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.

*****
*   *
*   *
*   *
*****
3

Output:

*****
*   *
*   *
*   *
*****
2


14. Right Pascal Star Pattern In Python

*****
*   *
*   *
*   *
*****
5

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

*****
*   *
*   *
*   *
*****
6

*****
*   *
*   *
*   *
*****
7

Output:

*****
*   *
*   *
*   *
*****
5


15. Left Pascal Star Pattern In Python

*****
*   *
*   *
*   *
*****
9

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.

# 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()
0

Output:

*****
*   *
*   *
*   *
*****
9


16. Heart pattern in python

# 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()
2

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.

# 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()
3

Output:

# 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()
2


17. Plus pattern program in Python

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

# 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()
5

The complete code is given below.

# 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()
6

Output:

# 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()
7


18. Cross pattern program in Python

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

# 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()
8

Here is the complete code to create the cross pattern.

# 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()
9

Output:

# 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()
8


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.

# 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) + '*')
1

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

# 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) + '*')
2

Output:

# 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) + '*')
1


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.

# 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) + '*')
4

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

# 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) + '*')
5

Output:

# 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) + '*')
4


21. Number Pyramid Pattern Program In Python

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

# 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) + '*')
7

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

# 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) + '*')
8

Output:

# 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) + '*')
7


22. Reverse Number Pyramid Pattern Program In Python

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

*****
*   *
*   *
*   *
*****
0

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

*****
*   *
*   *
*   *
*****
1

Output:

*****
*   *
*   *
*   *
*****
0


23. Hollow Number Pyramid Pattern Program

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

*****
*   *
*   *
*   *
*****
3

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

*****
*   *
*   *
*   *
*****
4

Output:

*****
*   *
*   *
*   *
*****
3


24. Number Diamond Pattern Program

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

*****
*   *
*   *
*   *
*****
6

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

*****
*   *
*   *
*   *
*****
7

Output:

*****
*   *
*   *
*   *
*****
6


25. Hollow Number Diamond Pattern Program

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

*****
*   *
*   *
*   *
*****
9

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

*
**
***
****
*****
0

Output:

*****
*   *
*   *
*   *
*****
9


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.

*
**
***
****
*****
2

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

*
**
***
****
*****
3

Output:

*
**
***
****
*****
2


27. Reverse Alphabet Pyramid Pattern Program

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

*
**
***
****
*****
5

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

*
**
***
****
*****
6

Output:

*
**
***
****
*****
5


28. Hollow Alphabet Pyramid Pattern

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

*
**
***
****
*****
8

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

*
**
***
****
*****
9

Output:

*
**
***
****
*****
8


29. Alphabet Diamond Pattern Program

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

# 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()
1

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

# 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()
2

Output:

# 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()
1


30. Hollow Alphabet Diamond Pattern

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

# 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()
4

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

# 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()
5

Output:

# 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()
4


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.