pattern program in python

In this article, let's learn how to write a Python program to print a pattern using star *. 

 

pattern program in python

 

Let's try to print a right angle triangle using a start so for this lets ask a user to enter the length of the pattern.  after getting input as a number so lets the user follow starting from 0 index to the length of the pattern and then use the star string to multiply with the index.

numbers = int(input("Enter a Number : "))
for index in range(numbers):
    print("* " * (index+1))


OUTPUT:

Enter a Number : 5
*
* *
* * *
* * * *
* * * * *



Let's try to print the number pattern in triangle format

Ask a user to enter the length of a pattern  using input function call.  after getting the input number use the for loop to get the index range from 0 to N and then try to print the numbers using  print function call converting the index to string with the space

numbers = int(input("Enter a Number : "))
for index in range(numbers):
   print((str(index+1) + " ")* (index+1))


OUTPUT:

Enter a Number : 5
1
2 2
3 3 3
4 4 4 4
5 5 5 5 5


Conclusion :  Try the program by yourself by modifying the the print statement and try to to see that changed let me know in the comments section if you need any other pattern program.


Post a Comment

0 Comments