Hollow Pyramid Star Pattern in Python

Hollow pyramid star pattern in Python | In the previous article, we had to print many pyramids star patterns using for loop and while loop but here, we have to print the hollow pyramid star patterns using for loop and while loop.

Hollow Pyramid Star Pattern in Python

Hollow Pyramid Star Pattern-1

Enter any number: 6

          * 
        *   * 
      *       * 
    *           * 
  *               * 
* * * * * * * * * * *
# Hollow pyramid star pattern in Python

# user-defined function
def pattern(n):
   for i in range(n):
      for j in range(n-i-1):
         # print the spaces
         print(" ", end=" ")
      
      for j in range(2*i+1):
         # printing stars
         if j == 0 or j == 2*i:
            # print the stars
            print("*", end=" ")
         else:
            if i == n-1:
               # print the stars
               print("*", end=" ")
            else:
               # print the spaces
               print(" ", end=" ")
      print()
 
# input from the user
n = int(input('Enter any number: '))

# the function call
pattern(n)

Print Hollow Pyramid using While Loop

# Hollow pyramid star pattern in Python

# user-defined function
def pattern(n):
   i=1 
   while i<=n: 
      if i==1: 
         # print the spaces
         print(" " * (n-i) + "*")
      elif i==n: 
         # print the stars
         print("* " * i)
      else: 
         # print the spaces and stars
         print(" " * (n-i) + "*" + " " * (2*i-3) + "*")
      i+=1

# input from the user
n = int(input('Enter any number: '))

# the function call
pattern(n)

Hollow Pyramid Star Pattern-2

Enter any number: 5

* 
* * 
*   * 
*     * 
* * * * * 
# Hollow left pyramid star pattern in Python

# user-defined function
def pattern(n):
   for i in range(1, n+1):
      for j in range(i):
         if j == 0 or j == i-1:
            # print the stars
            print("*", end=" ")
         else:
            if i != n:
               # print the spaces
               print(" ", end=" ")
            else:
               # print the stars
               print("*", end=" ")
      print()
 
# input from the user
n = int(input('Enter any number: '))

# the function call
pattern(n)

Print Hollow Left Half Pyramid using While Loop

# Hollow left pyramid star pattern in Python

# user-defined function
def pattern(n):
   i=1 
   while i<=n: 
      if i==1: 
         # print the spaces
         print("" * (n-i) + "*")
      elif i==n: 
         # print the stars
         print("* " * i)
      else: 
         # print the spaces and stars
         print("" * (n-i) + "*" + " " * (2*i-3) + "*")
      i+=1

# input from the user
n = int(input('Enter any number: '))

# the function call
pattern(n)

Hollow Pyramid Star Pattern-3

Enter any number: 7

* * * * * * * 
*         * 
*       * 
*     * 
*   * 
* * 
* 
# Hollow left downward pyramid star pattern in Python

# user-defined function
def pattern(n):
   for i in range(n, 0, -1):
      for j in range(i, 0, -1):
         if i == 1 or i == n or j == 1 or j == i:
            # print the stars
            print("*", end=" ")
         else:
            # print the spaces
            print(" ", end=" ")
      print()

# input from the user
n = int(input('Enter any number: '))

# the function call
pattern(n)

Hollow Pyramid Star Pattern-4

Enter any number: 4

  * * * * * * * 
    *       * 
      *   * 
        * 
# Hollow downward pyramid star pattern in Python

# user-defined function
def pattern(n):
   for i in range(1, n+1):
      for j in range(0, i):
         # print the spaces
         print(" ", end=" ")

      for j in range(1, (n*2 - (2*i-1)) + 1):
         if i == 1 or j == 1 or j ==(n*2 -(2*i-1)):
            # print the stars
            print("*", end=" ")
         else:
            # print the spaces
            print(" ", end=" ")
      print()

# input from the user
n = int(input('Enter any number: '))

# the function call
pattern(n)

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published. Required fields are marked *