To print a pyramid pattern in Python, you can use nested loops where the outer loop controls the rows and the inner loops control the spaces and stars printed in each row.
In this example,
print_pyramid
that takes an integer n
as input to represent the number of rows in the pyramid.print()
to move to the next line.def print_pyramid(n):
for i in range(1, n+1):
print(' ' * (n - i) + '*' * (2 * i - 1))
print_pyramid(5)
* *** ***** ******* *********
In this tutorial, we learned How to print Pyramid Pattern in Python language with well detailed examples.