Python program to print number with star pattern

We have to print the pattern as given in the below example.

Here is a Python program that prints a given number with a star pattern: 

 

def print_star_pattern(number):
    for i in range(1, number+1):
        for j in range(1, i+1):
            print("*", end="")
        print()
    for i in range(number-1, 0, -1):
        for j in range(1, i+1):
            print("*", end="")
        print()

In this program, we define a function called print_star_pattern that takes in a parameter number, which is the number we want to print in a star pattern. The program then uses two nested for loops to print the star pattern.

The first for loop starts from 1 and goes up to the value of number, and is responsible for printing the first half of the pattern. Inside this loop, we have another for loop that prints i number of stars for each iteration. The end parameter of the print function is set to an empty string so that each print statement prints on the same line.

After the first for loop, we have another for loop that starts from number-1 and goes down to 1, and is responsible for printing the second half of the pattern. The inner for loop inside this loop also prints i number of stars for each iteration.

We can then call this function with the number we want to print, like this: 

 

print_star_pattern(5)

This will print the number 5 in a star pattern like this: 

 

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

 

Submit Your Programming Assignment Details