Star Pattern in Python

Star pattern is the common question in programming exams and sometimes in interview questions also.

This can be tricky at some times because there are some complicated patterns too.

Although there are various patterns like pyramid, number, alphabet, asterisk pattern. We will see the printing of different patterns of stars in a simple manner. 

In Python, a for loop is used to print the various patterns. 

  • The outer loop to print the number of rows.
  • The inner loops to print the number of columns.
  • The variable to print white space according to the required place in Python.

We want you to know about the Star Pattern in Python, hence we are come up with the best video explanation you have ever watched on the internet, where you not only understand the logic but also code to get the desired output.

You still face difficulties in understanding the logic? That’s perfectly fine, we have something more for you. You can scroll down to learn more about this topic.

For all the practice Videos and Explanation on Python please click over here. Python Practice Series.

Video Explanation to Star pattern in Python

Code: Star pattern in Python

Method 1: Star Pyramid Pattern I

Expected Output:

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

Source code:

n = int(input("Enter the number of rows:"))  
for i in range(0, n):
    for j in range(0, i + 1):
            print("* ", end="")       
    print() 

Code Explanation Method 1: Star Pyramid Pattern I

In the above code, we have initialized the n variable we are accepting number from user and converting into int.

Then in the next line, we have written for loop with counter variable I, and this loop will run from 0 to n but as in range the last number is opted out hence loop will run till n-1.

This loop is written to print the number of rows. 

Inside the indent of the for loop we have another for loop written for the printing star. But look here carefully this loop is changing according to the outer loop.

The iteration of the inner for loop depends on the outer loop. The inner loop is responsible for printing the number of columns.

In the first iteration, the value of i is 0, and it increased by 1, so it becomes 0+1, now the inner loop iterated the first time and prints one star(*).

In the second iteration, the value of i is 1 and it increased by 1, so it becomes 1+1, now the inner loop iterated two times and prints two-star (* *).

Here if you look at the print statement here we have used the end parameter.

By default, the print method ends with a newline. This means there is no need to explicitly specify the parameter end as ‘\n’. 

But we want to print two or more stars in the same line hence we have defined a blank value to the end parameter hence the program won’t print the star on the next line.

The end argument prevents to jump into another line. It will print the star until the loop is valid.

The last print statement is inside the first for loop, it is  responsible for ending the line after each row means it will add the next line in the program.

Method 2 : Star Pyramid Pattern II

Expected Output:

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

Source Code:

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

Code Explanation Method 2: Star Pyramid Pattern II

In the above code, we have initialized the num_rows variable we are accepting number from user and converting into an int.

Then in the next line, we have written for loop with counter variable I, and this loop will run from 0 to 4 as the range’s last number is opted out hence the loop will run till 4.

This loop is written to print the number of rows. 

Inside the indent of the for loop, we have another for loop written for adding space before the star. Hence we have used the end parameter is blank. 

This loop will run till num_rows -i-1, so at the first iteration, we have num_rows value 5 and i have 0 value so the loop will run for 4 times, next as i will increase by one loop will run for 3 and so on.

Now we have a next for loop that is the same as we have used in the last program, which will print stars in the same line. The end argument prevents jumping into another line. It will print the star until the loop is valid.

The last print statement is inside the first for loop, it is responsible for ending the line after each row means it will add the next line in the program.

Method 3: Star Pyramid Pattern III

Expected Output:

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

Source Code:

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

Code Explanation Method 3: Star Pyramid Pattern III

Here we have a variable called rows with a defined value as 5.

In the above program, we have used a reverse nested loop, the first time we are using a reverse nested loop while printing star pattern.

Here we have for loop with i as counter and in range, we have used 3 parameters, at the first position we have initiated initial value it is rows +1 and loop will run till it gets 0 and at the last or 3rd position we have increment or step here we are defined -1 hence it is a decrementing loop.

This loop is used for printing pattern rows.

Inside this loop, we have another loop which is for printing stars. This is also a reverse loop here. We have initiated a loop at 0 and it will go till the value of i-1 so in the first iteration it is 6 hence it will go till 6-1 times and inside for we have a print statement to print star and end character as blank value for printing star on the same line.

The last print statement is inside the main loop and is used for printing the last line.

The last print statement is inside the first for loop, it is responsible for ending the line after each row means it will add the next line in the program.

Facebook Comments
(Visited 2 times, 1 visits today)

Leave A Comment

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