Pyramid Pattern Program in Python
Print a centred star pyramid of n rows in Python and understand the spacing formula behind it.
- Time
O(n^2), because the rows hold about n^2 characters in total - Space
O(n) for the row string; O(1) for the nested-loop version - Hints3
- Read4 min
To print a pyramid pattern in Python with n rows, loop i from 1 to n. On row i print n - i spaces, then 2 * i - 1 stars: print(" " * (n - i) + "*" * (2 * i - 1)). For n = 5 the rows have 1, 3, 5, 7 and 9 stars, and the spaces shrink from 4 to 0, which centres each row. The nested-loop version does the same thing with an inner loop for spaces and another for stars.
Problem statement
Write a pyramid pattern program in Python. Given n, print a centred pyramid of
stars with n rows. For n = 5:
*
***
*****
*******
*********
First work out how many spaces and stars each row needs, then write the loops.
An integer n, the number of rows.
n lines forming a centred star pyramid.
Examples
n = 3" *", " ***", "*****"Row 1: 2 spaces and 1 star. Row 2: 1 space and 3 stars. Row 3: 0 spaces and 5 stars.
n = 1"*"One row, no spaces and one star.
Constraints
- 1 <= n <= 50 (wider pyramids wrap on most screens)
Think before you code
Reveal the questions to ask yourself first
- Count the stars in each row of the n = 5 pyramid. What pattern do you see?
- Count the spaces before the first star in each row. How do they relate to n and i?
- Why does the last row have no spaces at all?
Hints
Open one at a time — try again after each.1 Hint 1
2 Hint 2
3 Hint 3
Approach
Reveal the step-by-step approach
The row table
Write down what each row needs before you write any loop. For n = 5:
| Row i | Spaces (n - i) | Stars (2i - 1) | Row |
|---|---|---|---|
| 1 | 4 | 1 | ····* |
| 2 | 3 | 3 | ···*** |
| 3 | 2 | 5 | ··***** |
| 4 | 1 | 7 | ·******* |
| 5 | 0 | 9 | ********* |
(The dots stand for spaces.) Each row adds one star on each side, so the star count goes up by 2, and the leading spaces go down by 1 to keep the middle star in the same column.
Spacing logic
The bottom row is the widest, with 2n - 1 stars and no spaces. Every row above it
is shifted right by one more space. That is why the spaces are n - i: row n gets
0, row n - 1 gets 1, and row 1 gets n - 1.
Two ways to print a row
- String repetition:
" " * (n - i) + "*" * (2 * i - 1)builds the row in one expression. - Nested loops: an inner loop prints one space at a time with
end="", a second inner loop prints the stars, andprint()ends the line. Exams often ask for this version because it works the same way in C and Java.
Dry run
Walk through the example step by step
n = 4, nested-loop version:
i | spaces = n - i | stars = 2i - 1 | printed
--+----------------+----------------+---------
1 | 3 | 1 | " *"
2 | 2 | 3 | " ***"
3 | 1 | 5 | " *****"
4 | 0 | 7 | "*******"
On row 2, the space loop runs 2 times, the star loop runs 3 times, then print()
moves to the next line.
Solution
Reveal the full Python solution
n = 5
for i in range(1, n + 1):
spaces = n - i
stars = 2 * i - 1
print(" " * spaces + "*" * stars)Output:
*
***
*****
*******
*********
Star pyramid in Python with nested loops
n = 4
for i in range(1, n + 1):
for s in range(n - i):
print(" ", end="")
for j in range(2 * i - 1):
print("*", end="")
print()
*
***
*****
*******
end="" stops print from moving to a new line after each character. The empty
print() at the end of the outer loop moves to the next row. This is the same
structure you would write in Java or C.
Pyramid with spaces between stars
Many books show the pyramid with a space after each star. Then each row needs i
stars, not 2i - 1, because the spaces already make it wider.
n = 5
for i in range(1, n + 1):
print(" " * (n - i) + " ".join("*" * i))
*
* *
* * *
* * * *
* * * * *
" ".join("*" * 3) gives "* * *", with spaces only between the stars and none at
the end of the line.
Inverted pyramid pattern in Python
Run the rows in reverse with range(n, 0, -1). The formulas stay the same.
n = 5
for i in range(n, 0, -1):
print(" " * (n - i) + "*" * (2 * i - 1))
*********
*******
*****
***
*
Number pyramid
Replace the stars with 1 2 ... i going up and i - 1 ... 1 coming back down.
n = 5
for i in range(1, n + 1):
left = "".join(str(k) for k in range(1, i + 1))
right = "".join(str(k) for k in range(i - 1, 0, -1))
print(" " * (n - i) + left + right)
1
121
12321
1234321
123454321
Each row still has 2i - 1 characters: i on the left part and i - 1 on the right.
More number shapes are on number pattern programs in Python.
With user input
rows = int(input("Enter number of rows: "))
for i in range(1, rows + 1):
print(" " * (rows - i) + "*" * (2 * i - 1))
Enter number of rows: 3
*
***
*****
For the half pyramid in C, see half pyramid pattern program in C.
O(n^2), because the rows hold about n^2 characters in totalO(n) for the row string; O(1) for the nested-loop versionReview before the interview
Common mistakes
- Printing i stars instead of 2 * i - 1, which gives a lopsided half pyramid.
- Printing i spaces instead of n - i, so the pyramid leans the wrong way.
- Forgetting end="" in the nested version, so every star appears on its own line.
- Forgetting the empty print() after the inner loops, so all rows join into one line.
- Using range(n) for rows. It starts at 0, prints a blank first row and stops one row short.
Edge cases to test
- n = 1: a single star with no spaces.
- n = 0: range(1, 1) is empty, so nothing is printed.
- Large n: the bottom row has 2n - 1 characters and may wrap in a narrow terminal.
- Spaced version: uses i stars per row, not 2i - 1.
Interview follow-ups
- How would you print a full diamond by joining a pyramid and an inverted pyramid?
- How would you print a hollow pyramid with stars only on the border?
- How would you print a pyramid of letters A, ABA, ABCBA?
Keep practising
Number Pattern Programs in Python with Output
Hints · Dry run · SolutionSolve Similar problemHalf Pyramid Pattern Program in C
Hints · Dry run · SolutionSolve Similar problemStar Pattern Programs in Java
Hints · Dry run · SolutionSolve Similar problemDiamond Pattern in Java
Hints · Dry run · SolutionSolve Related learningLoops In Programming
TutorialLearn Related learningWhat Is Python Used For
TutorialLearn Related learningPython Interview Questions
TutorialLearn CareerPilot AIPractice under pressure
Take an AI mock interview and get question-by-question feedback.
Mock interviewStartPractising Python for interviews?
CodeBegun’s Python Full Stack with AI program builds this problem-solving muscle with mentor review and mock interviews.
