Star Pattern Programs in Python using While Loop

Star pattern programs in Python using while loop | Here, we will write the python program to print star patterns. A star pattern is a pattern that consists of a series of stars. The number of stars is determined by the number of rows and columns.

Note:- Frist loop is used for displaying row stars and the inner loop is used to display columns (assume Matrix ). That’s why the number of rows is controlled by the outer or first loop but displaying values in those columns the inner or second loop is used. In these programs, we have used i, j as loop iterator variables (i => row,  j => column).

In this article, Some patterns and test cases are given, after observing those patterns we will write Python programs using the While Loop.

Star Pattern in Python using While Loop

Star Pattern Program 1

In the below pattern program, we will print the left half pyramid. We will print a single star in the first row, 2 stars in the second row and continue doing this in a similar fashion until we reach row N number. In the program, run 2 nested loops where the internal loop will run for N number of times as the N number of times the external loop has run and print star pattern.

Sample Input/ Output:-

Enter the number of rows: 3
*
* *
* * *

Enter the number of rows: 5
*
* *
* * *
* * * *
* * * * *

# Python program to print left half pyramid star pattern 

def pattern(n):
   i = 1
   while i <= n :
      j = 1
      while j <= i:
         # printing stars
         print("*", end=" ")
         j = j + 1
      print()
      i = i + 1
 
# take inputs
n = int(input('Enter the number of rows: '))

# calling function
pattern(n)

Short Methods

# Python program to print left half pyramid star pattern 

def pattern(n):
   i = 1
   while i<=n:
      # printing stars
      print("* " * i)
      i+=1 
 
# take inputs
n = int(input('Enter the number of rows: '))

# calling function
pattern(n)

Star Pattern Program 2

We will print the downward left half pyramid. The downward left half pyramid pattern is the same as the left half pyramid pattern but it is upside down.

Sample Input/ Output:-

Enter the number of rows: 6
* * * * * *
* * * * *
* * * *
* * *
* *
*

# Python program to print downward left half pyramid star pattern 

def pattern(n):
   i = 1
   while i <= n :
      j = n
      while j >= i:
         # printing stars
         print("*", end=" ")
         j = j - 1
      print()
      i = i + 1
 
# take inputs
n = int(input('Enter the number of rows: '))

# calling function
pattern(n)

Short Methods

# Python program to print downward left half pyramid star pattern 

def pattern(n):
   i = n
   while i>=1:
      # printing stars
      print("* " * i)
      i-=1 
 
# take inputs
n = int(input('Enter the number of rows: '))

# calling function
pattern(n)

Star Pattern Program 3

In this pattern, rotate the left half pyramid by 180 degrees so that we can get a right half pyramid. We have started printing stars from the right side.

Sample Input/ Output:-

Enter the number of rows: 7

      *
     **
    ***
   ****
  *****
 ******
*******
# Python program to print right half pyramid star pattern 

def pattern(n):
   i = 1
   while i<=n:
      # printing stars
      print(" "*(n-i) + "*" * i)
      i+=1 
 
# take inputs
n = int(input('Enter the number of rows: '))

# calling function
pattern(n)

Star Pattern Program 4

We will print the downward right half pyramid. The downward right half pyramid pattern is the same as the right half pyramid pattern but it is upside down.

Sample Input/ Output:-

Enter the number of rows: 6

******
 *****
  ****
   ***
    **
     *
# Python program to print downward right half pyramid star pattern 

def pattern(n):
   i = n
   while i>=1:
      # printing stars
      print(" "*(n-i) + "*" * i)
      i-=1 
 
# take inputs
n = int(input('Enter the number of rows: '))

# calling function
pattern(n)

Star Pattern Program 5

We will print a triangle or pyramid using the star pattern. Here we are running 1 while loops.

Sample Input/ Output:-

Enter the number of rows: 7

      * 
     * * 
    * * * 
   * * * * 
  * * * * * 
 * * * * * * 
* * * * * * * 
# Python program to print triangle star pattern 

def pattern(n):
   i = 1
   while i<=n:
      # printing stars
      print(" "*(n-i) + "* " * i)
      i+=1 
 
# take inputs
n = int(input('Enter the number of rows: '))

# calling function
pattern(n)

Star Pattern Program 6

The reverse triangle or pyramid pattern is the same as the triangle pattern but it is upside down.

Sample Input/ Output:-

Enter the number of rows: 7

* * * * * * * 
 * * * * * * 
  * * * * * 
   * * * * 
    * * * 
     * * 
      * 
# Python program to print downward triangle star pattern 

def pattern(n):
   i = n
   while i>=1:
      # printing stars
      print(" "*(n-i) + "* " * i)
      i-=1 
 
# take inputs
n = int(input('Enter the number of rows: '))

# calling function
pattern(n)

Star Pattern Program 7

In this pattern, we will print the right pascal triangle. Print two half pyramids, one left half pyramid, and the second downward left half pyramid.

Sample Input/ Output:-

Enter the number of rows: 5
*
* *
* * *
* * * *
* * * * *
* * * *
* * *
* *
*

# Python program to print right pascal triangle star pattern 

def pattern(n):
   # print upper triangle
   i = 1
   while i<n:
      # printing stars
      print("* " * i)
      i+=1

   # print lower triangle
   i = n
   while i>=1:
      # printing stars
      print("* " * i)
      i-=1
 
# take inputs
n = int(input('Enter the number of rows: '))

# calling function
pattern(n)

Star Pattern Program 8

We will print the left pascal triangle. Print two half pyramids, one right half pyramid, and the second downward right half pyramid.

Sample Input/ Output:-

Enter the number of rows: 6

     *
    **
   ***
  ****
 *****
******
 *****
  ****
   ***
    **
     *
# Python program to print left pascal triangle star pattern 

def pattern(n):
   # print upper triangle
   i = 1
   while i<n:
      # printing stars
      print(" "*(n-i) + "*" * i)
      i+=1

   # print lower triangle
   i = n
   while i>=1:
      # printing stars
      print(" "*(n-i) + "*" * i)
      i-=1
 
# take inputs
n = int(input('Enter the number of rows: '))

# calling function
pattern(n)

Star Pattern Program 9

In the below program, we will print a diamond star pattern. Print two triangles, one triangle, and the second reverse triangle.

Sample Input/ Output:-

Enter the number of rows: 6

     * 
    * * 
   * * * 
  * * * * 
 * * * * * 
* * * * * * 
 * * * * * 
  * * * * 
   * * * 
    * * 
     * 
# Python program to print diamond star pattern 

def pattern(n):
   # print upper triangle
   i = 1
   while i<n:
      # printing stars
      print(" "*(n-i) + "* " * i)
      i+=1 

   # print lower triangle
   i = n
   while i>=1:
      # printing stars
      print(" "*(n-i) + "* " * i)
      i-=1
 
# take inputs
n = int(input('Enter the number of rows: '))

# calling function
pattern(n)

Star Pattern Program 10

We will print an hourglass star pattern. Print two triangles, one reverse triangle, and the second triangle.

Sample Input/ Output:-

Enter the number of rows: 7

* * * * * * * 
 * * * * * * 
  * * * * * 
   * * * * 
    * * * 
     * * 
      * 
     * * 
    * * * 
   * * * * 
  * * * * * 
 * * * * * * 
* * * * * * *
# Python program to print hourglass star pattern 

def pattern(n):
   # print upper triangle
   i = n
   while i>1:
      # printing stars
      print(" "*(n-i) + "* " * i)
      i-=1

   # print lower triangle
   i = 1
   while i<=n:
      # printing stars
      print(" "*(n-i) + "* " * i)
      i+=1
 
# take inputs
n = int(input('Enter the number of rows: '))

# calling function
pattern(n)

Star Pattern Programs in Python using While Loop

Star Pattern Program 11

The hollow square pattern programs are a bit more difficult than simple star pattern programs because here you will have to deal with spaces.

Sample Input/ Output:-

Enter the number of rows: 6

*
* *
*   *
*     *
*       *
* * * * * *
# Python program to print hollow left half pyramid star pattern 

def pattern(n):
   i=1 
   while i<=n: 
      if i==1: 
         print("" * (n-i) + "*")
      elif i==n: 
         print("* " * i)
      else: 
         print("" * (n-i) + "*" + " " * (2*i-3) + "*")
      i+=1

# take inputs
n = int(input('Enter the number of rows: '))

# calling function
pattern(n)

Star Pattern Program 12

Sample Input/ Output:-

Enter the number of rows: 5

    *
   * *
  *   *
 *     *
* * * * *
# Python program to print hollow triangle star pattern 

def pattern(n):
   i=1 
   while i<=n: 
      if i==1: 
         print(" " * (n-i) + "*")
      elif i==n: 
         print("* " * i)
      else: 
         print(" " * (n-i) + "*" + " " * (2*i-3) + "*")
      i+=1

# take inputs
n = int(input('Enter the number of rows: '))

# calling function
pattern(n)

Also See:- Star Pattern Programs in Python using For Loop

Leave a Comment

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