Print Pyramid Pattern
Print a centered pyramid of stars using nested loops.
BeginnerTopic: Loop Programs
Python Print Pyramid Pattern Program
This program helps you to learn the fundamental structure and syntax of Python programming.
# Program to print a pyramid star pattern
rows = int(input("Enter number of rows: "))
for i in range(1, rows + 1):
spaces = " " * (rows - i)
stars = "*" * (2 * i - 1)
print(spaces + stars)Output
Enter number of rows: 3 * *** *****
Understanding Print Pyramid Pattern
We center each row by printing leading spaces and then an odd number of stars: 2*i - 1.
Note: To write and run Python programs, you need to set up the local environment on your computer. Refer to the complete article Setting up Python Development Environment. If you do not want to set up the local environment on your computer, you can also use online IDE to write and run your Python programs.