Your Ultimate Guide to Star Pattern Printing in Python
Your Ultimate Guide to Star Pattern Printing in Python

Pattern printing in any programming language is an essential step in understanding the practicality of the language. It helps in making us more accommodating to the particular language.

In this tutorial, I’ll be printing star(*) patterns in Python. Star patterns are the most common yet tricky patterns at times. They can be printed with the help of “for loops” in python.

BASIC LOGIC BEHIND PATTERN PRINTING

Irrespective of the language you’re using to print patterns, just keep this basic working concept in mind:

  • Generally, a minimum of 2 loops is required to print any pattern of any type. One is the parent or the outer loop, which is followed by the nested or the child loop.
  • The outer loop is the direct indication of the number of rows or the horizontal lines that are required in a particular pattern.
  • The inner loop depicts the number of columns or the vertical lines that are required.

Now that we have a basic foundation on how this cycle of pattern printing works, we can start printing our patterns. I use VS Code for all my projects.

Half Pyramid Pattern

# number of rows
num = 5
for i in range(0, num):
    # nested loop for each column
    for j in range(0, i + 1):
        print("*", end=' ') # print *
    print("\r") # new line after each row

Alternate Solution:

for i in range(1,6): 
    print("* " * i)

Output:

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

Downward Half Pyramid Pattern

# number of rows
num = 5
# executing outer loop in reverse order
for i in range(num + 1, 0, -1):    
    for j in range(0, i - 1):  
        print("*", end=' ') # print *
    print(" ")  # new line after each row

Output:

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

Right Triangle Pyramid Pattern

# number of rows
rows = 5
k = 2 * rows - 2
for i in range(0, rows):
    # for each column
    for j in range(0, k):
        # print space between stars
        print(end=" ")
    k = k - 2
    for j in range(0, i + 1):
        # display star
        print("* ", end="")
    print("")

Output:

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

Equilateral Triangle Pattern

# number of rows
n = 5
k = n - 1 
for i in range(0, n):
    # for each column
    for j in range(0, k):
        # print space between stars
        print(end=" ")
    k = k - 1
    for j in range(0, i + 1):
        # display star
        print("* ", end="")
    print("")

Output:

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

Square Shaped Pattern

# number of rows
num = 5
for i in range(num):
    for j in range(num):
        print("*", end=' ')
    print(" ")

Alternate Solution:

# using concatenation and multiplication
num = 5
for i in range(num):
    print("* "*num)

Output:

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

Christmas Tree Pattern

# number of rows
num = 5
for i in range(1, num + 1):
    print(" "*(num -i)+"* "* i)
for i in range(1, num + 1):
    print(" "*(num -i)+"* "* i)

Output:

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

Diamond Shaped Pattern

# number of rows
num = 5
for i in range(1, num + 1):
    print(" "*(num -i)+"* "* i)
for i in range(num-1,0,-1):
    print(" "*(num -i)+"* "* i)

Output:

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

Downward Triangle Pattern

# number of rows
num = 6
for i in range(num,0,-1):
    print(" "*(num -i)+"* "* i)

Output:

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

Tilted Triangle Pattern

# number of rows
num = 6
for i in range(num,0,-1):
    print(" "*(num + i)+"* "* i)

Output:

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

Advanced Pattern Printing: Includes Logical Operators and Conditional Statements

Alphabet “A” Pattern

# number of rows
for row in range(7):
    for col in range(5):
        if ((col==0 or col==4) and row!=0) or ((row==0 or row==3) and (col>0 and col<4)):
            print("*", end="")
        else:
            print(end=" ")
    print()

Output:

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

Heart-Shaped Pattern

# number of rows
for row in range(6):
    for col in range(7):
        if (row==0 and col%3!=0) or (row==1 and col%3==0) or (row-col==2) or (row+col==8):
            print("*", end="")
        else:
            print(end=" ")
    print()

Output:

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

Printing patterns like these require some good understanding of python’s if-else conditional statements and logical operators.

If I break down the concept involved, draw a grid-like structure depicting the rows and columns. Then simply draw the shape you are targeting in the form of stars. This will give you a clear idea of what row and column are filled with stars and what is filled with empty space. Once you are clear till here, jot down the empty areas and star-occupied spaces, and apply the conditional statements accordingly.

Thank you for giving this article your time! I hope it helped you somewhere.

Please check out my article on this amazing and powerful python library that lets you convert your text to handwriting in no time. Make sure to have a look at my other articles here.


Vaishali Rastogi

Hey There! I am Vaishali Rastogi and I am a tech-researcher. I've been doing writing for a good 4-5 years, so here I am now. Working on my own website to make people digitally aware of the updates in technology.

0 Comments

Leave a Reply

Avatar placeholder

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