Mirrored Right Triangle Star Pattern in Python | In this article, we have to write a python program to print the right triangle star pattern using the for loop and while loop.
Example of mirrored right triangle star pattern:-
*
* *
* * *
* * * *
* * * * *
Python Program to Print Mirrored Right Triangle Star Pattern
In this program, we nested 3 for loops to print the right triangle star pattern. Where one for loop will print row values, and two for loop will print column values.
# Mirrored right triangle star pattern in Python
# take input
n = 5
# printing mirrored right triangle
for i in range(n):
for j in range(n-i-1):
# printing space
print(" ", end=" ")
for j in range(i+1):
# printing star
print("* ",end="")
print()
Output:-
*
* *
* * *
* * * *
* * * * *
In the previous program, inputs are hardcoded in the program but in this program, inputs will be provided by the user.
# Mirrored right triangle star pattern in Python
# take input
n = int(input('Enter the number of rows: '))
# printing mirrored right triangle
for i in range(n):
for j in range(n-i-1):
# printing space
print(" ", end=" ")
for j in range(i+1):
# printing star
print("* ",end="")
print()
Output:-
Enter the number of rows: 6
*
* *
* * *
* * * *
* * * * *
* * * * * *
We can also take the help of a user-defined function to print mirrored right triangle star pattern in Python. A function is a block of code that performs a specific task.
# Mirrored right triangle star pattern in Python
def pattern(n):
for i in range(n):
for j in range(n-i-1):
# printing space
print(" ", end=" ")
for j in range(i+1):
# printing star
print("* ",end="")
print()
# take input
n = int(input('Enter the number of rows: '))
# calling function
pattern(n)
Mirrored Right Triangle Star Pattern in Python
We will print the same pattern but with a different way to write this python program. In this pattern program, we will print mirrored right triangle in python using only 1 for loop.
# Mirrored right triangle star pattern in Python
def pattern(n):
for i in range(1, n+1):
# printing space and star
print(" " * (n-i) + "* " * i)
# take input
n = int(input('Enter the number of rows: '))
# calling function
pattern(n)
Output:-
Enter the number of rows: 4
*
* *
* * *
* * * *
Right Triangle Star Pattern in Python using While Loop
In the previous pattern program, we have used the For Loop to print mirrored right triangle star pattern but in this pattern program, we will use the While Loop.
# Mirrored right triangle star pattern in Python
def pattern(n):
i = 1
while i<=n:
# printing space and star
print(" "*(n-i) + "* " * i)
i+=1
# take input
n = int(input('Enter the number of rows: '))
# calling function
pattern(n)
Output:-
Enter the number of rows: 8
*
* *
* * *
* * * *
* * * * *
* * * * * *
* * * * * * *
* * * * * * * *
Also See:- Pattern Programs in C