22.4 C
New York
Friday, April 26, 2024
HomeProgrammingPythonPattern Programs in Python

Pattern Programs in Python [Star and Alphabetical Patterns]

This tutorial will teach you how to print various stars and alphabetical patterns in Python. Explore these pattern programs in Python and learn how nested for loops work.

If you are looking for some of the best Python books, please don’t forget to check out this list.

Part 1: Star pattern programs in Python

pattern programs in python

1. Pyramid pattern program

# This program prints a pyramid pattern using a for loop

# Use a for loop to iterate over the number of rows in the pattern
for row in range(1, 6):
    # Use another for loop to print the number of spaces in each row
    for space in range(5 - row):
        print(" ", end="")
    # Use another for loop to print the number of stars in each row
    for star in range(2 * row - 1):
        print("*", end="")
    # After each row, print a newline character
    print()

This program uses a nested for loop to print the pyramid pattern. The outer for loop is used to iterate over the number of rows in the pattern, and the inner for loops is used to print the number of spaces and stars in each row.

The end parameter of the print() function is set to an empty string to prevent the spaces and stars from being printed on separate lines. This produces the following output:

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

2. Hourglass Pattern

Here is a Python program that uses a for loop to print an hourglass pattern:

# This program prints an hourglass pattern using a for loop

# Use a for loop to iterate over the number of rows in the pattern
for row in range(1, 6):
    # If the current row is in the first or last half of the pattern,
    # print the number of spaces and stars in the current row
    if row <= 3:
        for space in range(3 - row):
            print(" ", end="")
        for star in range(2 * row - 1):
            print("*", end="")
    # If the current row is in the middle of the pattern,
    # print a row of 7 stars
    else:
        for star in range(7):
            print("*", end="")
    # After each row, print a newline character
    print()

This program uses a nested for loop to print the hourglass pattern. The outer for loop is used to iterate over the number of rows in the pattern, and the inner for loop is used to print the number of spaces and stars in each row.

The “end” parameter of the “print()” function is set to an empty string to prevent the spaces and stars from being printed on separate lines. This produces the following output:

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

As with the other patterns, this is just one way to print an hourglass pattern using Python. Many other variations and techniques can achieve different patterns and effects.

3. Diamond Shape Pattern

Here is a simple program that prints a diamond-shaped pattern in Python:

# define the size of the diamond
n = 5

# use a for loop to print the top half of the diamond
for i in range(1, n+1):
  # print spaces on the left side of the diamond
  for j in range(1, n-i+1):
    print(" ", end="")
  # print stars in the middle of the diamond
  for j in range(1, 2*i):
    print("*", end="")
  # print spaces on the right side of the diamond
  for j in range(1, n-i+1):
    print(" ", end="")
  # move to the next line
  print()

# use a for loop to print the bottom half of the diamond
for i in range(1, n):
  # print spaces on the left side of the diamond
  for j in range(1, i+1):
    print(" ", end="")
  # print stars in the middle of the diamond
  for j in range(1, 2*(n-i)-1):
    print("*", end="")
  # print spaces on the right side of the diamond
  for j in range(1, i+1):
    print(" ", end="")
  # move to the next line
  print()

This program uses nested for loops to print the diamond pattern. The first for loop is used to print the top half of the diamond, and the second for loop is used to print the bottom half.

Each line of the diamond is printed using three for loops: one to print the spaces on the left side of the diamond, one to print the stars in the middle of the diamond, and one to print the spaces on the right side of the diamond.

The number of spaces and stars on each line of the diamond is determined by the loop variables i and j, as well as the size of the diamond (n).

If you want to change the size of the diamond, you can change the value of n at the beginning of the program. For example, if you want to print a larger diamond, you could set n = 7 to print a diamond that is 7 rows tall. This produces the following output:

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

4. Square Star Pattern

Here is one way to create a square star pattern in Python using nested for loops:

size = 5

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

This will produce the following output:

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

The outer for loop is used to iterate over the rows of the pattern, and the inner for loop is used to iterate over the columns.

In each iteration, we check if the current row and column indices (i and j) satisfy any of the conditions for printing a star and print a star if they do. Otherwise, we print a space.

5. Reverse Star Pattern in Python

To create a reverse star pattern in Python, you can use it for loops and print statements. Here is an example:

for i in range(5, 0, -1):
    for j in range(0, i):
        print("*", end="")
    print()

This code will print the following pattern:

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

6. Triangle Star Pattern in Python

To create a triangle star pattern in Python, you can use for loops and print statements. Here is an example:

for i in range(1, 6):
    for j in range(0, i):
        print("*", end="")
    print()

This code will print the following pattern:

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

The outer for loop controls the number of rows in the pattern, and the inner for loop prints the stars in each row. The end parameter in the print statement is used to prevent the stars in each row from being printed on separate lines.

To create a triangle star pattern with the base at the top, you can use a similar approach, but you will need to reverse the range in the outer for loop, like this:

for i in range(5, 0, -1):
    for j in range(0, i):
        print("*", end="")
    print()

This code will print the following pattern:

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

You can also create a triangle star pattern with the base at the bottom, like this:

for i in range(1, 6):
    for j in range(0, 6 - i):
        print(" ", end="")
    for j in range(0, i):
        print("*", end="")
    print()

This code will print the following pattern:

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

In this example, the first inner for loop prints the spaces before the stars in each row, and the second for loop prints the stars in each row. The end parameter in the print statement is used to prevent the spaces and stars in each row from being printed on separate lines.

7. Hollow Square Star Pattern in Python

To print a hollow square star pattern with a diagonal in Python, you can try the following code:

size = int(input("Enter the size of the square: "))

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

This code will ask the user to enter the size of the square and then print the square of the specified size with a hollow center and diagonal.

Here is an example output for a size of 5:

Enter the size of the square: 5
*****
*   *
* *
*   *
*****

Note that the square’s diagonal is formed by the “*” characters at the positions where i == j or i+j == size-1.

8. Hollow Pyramid Star Pattern in Python

To make the pyramid hollow, you can modify the code only to print the stars on the first and last rows and to only print the outermost stars on the other rows:

# define the number of rows in the pyramid
rows = 5

# loop through each row
for i in range(1, rows + 1):
  # print the spaces on the left side of the pyramid
  print(" " * (rows - i), end="")

  # if it's the first or last row, print the full row of stars
  if i == 1 or i == rows:
    print("*" * (i * 2 - 1), end="")
  # otherwise, only print the outermost stars on the row
  else:
    print("*" + " " * (i * 2 - 3) + "*", end="")

  # print the spaces on the right side of the pyramid
  print(" " * (rows - i))

This code will produce the following output:

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

Also ReadHow to Fix Unexpected EOF While Parsing Error In Python

Part 2: Alphabet pattern programs in Python

1. Right Triangle Alphabet Pattern

Here’s how you might create a right triangle alphabet pattern using Python:

# Print a new line at the start of each row
for i in range(26):
  print()

  # Print the letters for the current row
  for j in range(i + 1):
    c = chr(ord('A') + j)
    print(c, end=" ")

This code will print a right triangle pattern with 26 rows, each containing the letters of the alphabet from A to the current row number. For example, the first row will contain only the letter A, the second row will contain the letters A and B, and so on.

Output:

A 
A B 
A B C 
A B C D 
A B C D E 
A B C D E F 
A B C D E F G 
A B C D E F G H 
A B C D E F G H I 
A B C D E F G H I J 
A B C D E F G H I J K 
A B C D E F G H I J K L 
A B C D E F G H I J K L M 
A B C D E F G H I J K L M N 
A B C D E F G H I J K L M N O 
A B C D E F G H I J K L M N O P 
A B C D E F G H I J K L M N O P Q 
A B C D E F G H I J K L M N O P Q R 
A B C D E F G H I J K L M N O P Q R S 
A B C D E F G H I J K L M N O P Q R S T 
A B C D E F G H I J K L M N O P Q R S T U 
A B C D E F G H I J K L M N O P Q R S T U V 
A B C D E F G H I J K L M N O P Q R S T U V W 
A B C D E F G H I J K L M N O P Q R S T U V W X 
A B C D E F G H I J K L M N O P Q R S T U V W X Y 
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z

2. Left Triangle Alphabet Pattern

Here’s how you might create a left triangle alphabet pattern using Python:

Code:

for i in range(1, 6):
    for j in range(65, 65+i):
        print(chr(j), end=" ")
    print("")

Output:

A 
A B 
A B C 
A B C D 
A B C D E

Also Read10 Best Programming Apps To Learn Python

3. Hollow Rectangle Alphabet Pattern

In Python, you can use nested for loops and conditional statements to create a hollow rectangle alphabet pattern.

Here is an example:

# define the size of the pattern
size = 5

# use two nested for loops to iterate over the rows and columns
for i in range(size):
    for j in range(size):
        # if the current row is equal to the first or last row, or
        # the current column is equal to the first or last column,
        # print the alphabet
        if i == 0 or i == size - 1 or j == 0 or j == size - 1:
            print("A", end="")
        else:
            # otherwise, print a space
            print(" ", end="")
    # after each row is complete, print a newline
    print()

This code will produce the following output:

AAAAA
A   A
A   A
A   A
AAAAA

Note that you can adjust the pattern size by changing the value of the size variable. You can also modify the code to use different letters or symbols in the pattern.

4. Hollow pyramid alphabet pattern

To create a hollow pyramid pattern using characters, you can use a similar approach as the one described above. The only difference is that instead of using asterisks to create the pattern, you can use a different character.

Here is an example of how you can create a hollow pyramid pattern using the letter “X” as the character:

# Get the number of rows from the user
num_rows = int(input("Enter the number of rows: "))

# Use a nested for loop to print the pyramid pattern
for i in range(1, num_rows + 1):
    # Print the spaces
    for j in range(num_rows - i):
        print(" ", end="")

    # Print the Xs
    for j in range(2 * i - 1):
        if j == 0 or j == 2 * i - 2:
            print("X", end="")
        else:
            print(" ", end="")

    # Move to the next line
    print()

This code will print a hollow pyramid pattern using the letter “X” as the character. The pattern will have a specified number of rows. For example, if the user enters 5, the pattern will look like this:

    X
   X X
  X   X
 X     X
XXXXXXXXX

As before, you can customize the code to make the pattern look precisely how you want it to.

You can change the character used to create the pattern, the number of spaces between the characters, or any other pattern aspect.

5. Diamond alphabet pattern

You can use nested for loops to create a diamond alphabet pattern in Python. The outer loop will be used to print the rows, and the inner loop will be used to print the columns.

You can use the print() function to print the pattern, combining spaces and letters to create the diamond shape.

Here is an example of how you can create a diamond alphabet pattern in Python:

# Get the number of rows from the user
num_rows = int(input("Enter the number of rows: "))

# Use a nested for loop to print the diamond pattern
for i in range(-num_rows + 1, num_rows):
    # Calculate the absolute value of the current row
    row = abs(i)

    # Print the spaces
    for j in range(num_rows - row):
        print(" ", end="")

    # Print the letters
    for j in range(2 * row + 1):
        # Calculate the ASCII code for the current letter
        letter = ord("A") + row - abs(row - j)

        # Print the letter
        print(chr(letter), end="")

    # Move to the next line
    print()

This code will print a diamond alphabet pattern with the specified number of rows. For example, if the user enters 5, the pattern will look like this:

    A
   B B
  C   C
 D     D
E       E
 D     D
  C   C
   B B
    A

You can customize the code further to make the diamond pattern look precisely how you want it to. For example, you can change the number of spaces between the letters or use a different character instead of letters to create the pattern.

Also ReadPython Program To Check If A Number Is a Perfect Square

6. Reverse Pyramid Alphabet Pattern

You can use nested for loops to create a reverse pyramid alphabet pattern in Python.

The outer loop will be used to print the rows, and the inner loop will be used to print the columns. You can use the print() function to print the pattern, combining spaces and letters to create the reverse pyramid shape.

Here is an example of how you can create a reverse pyramid alphabet pattern in Python:

# Get the number of rows from the user
num_rows = int(input("Enter the number of rows: "))

# Use a nested for loop to print the reverse pyramid pattern
for i in range(num_rows, 0, -1):
    # Print the spaces
    for j in range(num_rows - i):
        print(" ", end="")

    # Print the letters
    for j in range(2 * i - 1):
        # Calculate the ASCII code for the current letter
        letter = ord("A") + i - 1

        # Print the letter
        print(chr(letter), end="")

    # Move to the next line
    print()

This code will print a reverse pyramid alphabet pattern with the specified number of rows.

For example, if the user enters 5, the pattern will look like this:

Enter the number of rows: 5
EEEEEEEEE
 DDDDDDD
  CCCCC
   BBB
    A

7. Heart Pattern

Here is a Python program that can print a heart character pattern:

# Get the total number of rows from the user
num_rows = int(input("Enter the number of rows: "))

# Create a loop to iterate over the number of rows
for i in range(1, num_rows+1):
    # Create another loop to print the spaces in the current row
    for j in range(1, num_rows-i+1):
        # Print a space character at the current position
        print(" ", end="")

    # Create another loop to print the heart character in the current row
    for j in range(1, i*2):
        # Print the heart character at the current position
        print("❤️", end="")

    # Move to the next line after printing the current row
    print()

Output:

Enter the number of rows: 4
   ❤️
  ❤️❤️❤️
 ❤️❤️❤️❤️❤️
❤️❤️❤️❤️❤️❤️❤️

This program works as follows:

  • First, it asks the user to input the number of rows to be printed in the heart pattern.
  • Then, it uses a loop to iterate over the number of rows. For each row, it creates two loops to print the spaces and the heart character.
  • The first loop prints the spaces in the current row, and the second loop prints the heart character in the current row. The number of spaces in each row equals the number of rows minus the current row number, and the number of heart characters in each row equals twice the current row number.
  • In each iteration of the inner loops, the program prints the character at the current position and then moves to the following line after printing the current row.
  • The end=” ” parameter in the print() function prints the characters in the same line, with a space between each character.
Himanshu Tyagi
Himanshu Tyagi
Hello Friends! I am Himanshu, a hobbyist programmer, tech enthusiast, and digital content creator. With CodeItBro, my mission is to promote coding and help people from non-tech backgrounds to learn this modern-age skill!
RELATED ARTICLES