DEV Community

Satish Chandra Gupta
Satish Chandra Gupta

Posted on • Edited on

Pattern Program In Python

Python has become the most rapidly growing programming language. Its use case is vast from handing server side to building machine learning models.

Loops➿ in Python have the most important role because it repeatedly does a certain task that we need.

As a beginner, you must practice loops a lot🧘.
Let's create some star, number, and alphabet pattern programs in Python to learn loops and conditional in a better way.

Originally posted at TutorialsTonight: Pattern program in Python

1. Square Pattern

*****
*****
*****
*****
*****
Enter fullscreen mode Exit fullscreen mode

Let's start with the simplest one. A hollow square pattern using stars.
To create this pattern execute a nested for loop in Python and print star at each iteration of the internal loop. At the end of the internal loop print a new line.
Here is the program:

# size of the square is 5 here
# 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()
Enter fullscreen mode Exit fullscreen mode

2. Hollow Square Pattern

*****
*   *
*   *
*   *
*****
Enter fullscreen mode Exit fullscreen mode

Let's complicate the thing a little further. Now You have to print a square pattern but from hollow inside.
Try yourself first, then check the solution 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()
Enter fullscreen mode Exit fullscreen mode

3. Pyramid Pattern

    *
   ***
  *****
 *******
*********
Enter fullscreen mode Exit fullscreen mode

Pyramid is quite famous in patterns. Let's create it using Python.

# 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()
Enter fullscreen mode Exit fullscreen mode

4. Hollow Pattern Program

    *
   * *
  *   *
 *     *
*********
Enter fullscreen mode Exit fullscreen mode

Complexing the thing a little further. Now you have to create a pattern program but from hollow inside.
Here is the Python program for this.

# 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()
Enter fullscreen mode Exit fullscreen mode

5. Hourglass Pattern

*********
 *******
  *****
   ***
    *
   ***
  *****
 *******
*********
Enter fullscreen mode Exit fullscreen mode

Here is a Python program for the hourglass pattern.

# 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()
Enter fullscreen mode Exit fullscreen mode

6. Heart Pattern

 ***   ***
***** *****
***********
 *********
  *******
   *****
    ***
     *
Enter fullscreen mode Exit fullscreen mode

Here is the heart pattern program in Python.

# 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()
Enter fullscreen mode Exit fullscreen mode

7. Plus Pattern

  *  
  *  
*****
  *  
  *
Enter fullscreen mode Exit fullscreen mode

Here is the program for + pattern in Python.

# 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()
Enter fullscreen mode Exit fullscreen mode

8. Cross pattern

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


Here is the cross pattern program in Python.


```python
# 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()
Enter fullscreen mode Exit fullscreen mode

9. Number Pyramid Pattern

    1
   123
  12345
 1234567
123456789
Enter fullscreen mode Exit fullscreen mode

Here is the Python program for the number pyramid pattern.

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

10. Alphabet Pyramid Pattern

    A
   ABC
  ABCDE
 ABCDEFG
ABCDEFGHI
Enter fullscreen mode Exit fullscreen mode

Here is the Python program for the alphabet pyramid pattern.

# 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()
Enter fullscreen mode Exit fullscreen mode

Top comments (0)