To print a Diamond Pattern in Python, we use nested loops. The outer loop controls the rows and the inner loops control the spaces and stars (*) to form the pattern.
In this example,
print_diamond
that takes the desired number of rows n
as a parameter.for
loops, where the outer loop runs from 1 to n
and the inner loops control the spaces and stars in each row.def print_diamond(n):
for i in range(1, n + 1):
spaces = ' ' * (n - i)
stars = '*' * (2 * i - 1)
print(spaces + stars)
for i in range(n - 1, 0, -1):
spaces = ' ' * (n - i)
stars = '*' * (2 * i - 1)
print(spaces + stars)
# Example usage
print_diamond(5)
* *** ***** ******* ********* ******* ***** *** *
In this tutorial, we learned How to print Diamond Pattern in Python language with well detailed examples.