How to Print a Pyramid of Stars in Python
In this blog, we will learn how to print a pyramid of stars using Python. The program prompts the user to enter the number of rows they want the pyramid to have, and then prints the pyramid using a loop and some simple arithmetic.
Let’s start by looking at the complete code:
n = int(input("Enter the number of rows : "))
for i in range(n):
print(" "*(n-1-i) + "* ", end="")
if i>0:
print(" "*(2*i-1) + "* ",end="")
print()
The program consists of two parts: the first part prompts the user for input, and the second part prints the pyramid. Let’s go through each part in detail.
Part 1: Prompt the User for Input
The first line of the program prompts the user to enter an integer value that represents the number of rows they want the pyramid to have:
n = int(input("Enter the number of rows : "))
The input()
function is used to accept user input, and int()
is used to convert the input to an integer. The value entered by the user is stored in the variable n
.
Part 2: Print the Pyramid
The second part of the program prints the pyramid using a loop and some simple arithmetic. Here is the code that does that:
for i in range(n):
print(" "*(n-1-i) + "* ", end="")
if i>0:
print(" "*(2*i-1) + "* ",end="")
print()
The loop iterates n
times, where n
is the number of rows entered by the user. The loop variable i
takes on values from 0
to n-1
, inclusive. For each iteration of the loop, the program prints a certain number of spaces, followed by one or two stars, depending on the row number.
Let’s go through each line of the loop in detail:
print(" "*(n-1-i) + "* ", end="")
: This line prints a certain number of spaces, followed by a single star, on the same line. The number of spaces printed is(n-1-i)
, wherei
is the current value of the loop variable. Theend=""
argument is used to suppress the newline character that is normally printed at the end of aprint()
statement.if i>0:
: This line starts anif
statement that checks whether the current value ofi
is greater than0
. This is done to avoid printing the extra set of spaces and stars on the first row, which should only have one star.print(" "*(2*i-1) + "* ",end="")
: This line prints an extra set of spaces and a star, on the same line, for all rows except the first one. The number of spaces printed is(2*i-1)
, wherei
is the current value of the loop variable.print()
: This line prints a newline character, which moves the cursor to the next line and starts the next iteration of the loop.
The program combines the spaces and stars in a way that creates a pyramid-like shape. The number of spaces decreases by one for each row, while the number of stars increases by two for each row.
Here is an example output of the program for n=5
:
*
* *
* *
* *
* *
As you can see, the program prints a pyramid of stars with five rows, where each row is centered and has the correct number of spaces and stars.
That’s it for this blog! I hope you found it helpful and that you now have a better understanding of how to print a pyramid of stars using Python.
Happy coding…
Thank you for taking the time to read my article. If you found it helpful, please consider sharing it with your friends and followers on social media. I’d also love to hear your thoughts on the article, so please leave a comment below with your feedback, questions, or suggestions.