Skip to main content
  • Python is available now. All other compilers are coming soon.

    Python
  • Popular Tutorials

    • Getting Started With Python
    • Python if Statement
    • while Loop in Python
    • Python Lists
    • Dictionaries in Python
    All Python Tutorials

    Reference Materials

    Built-in FunctionsList MethodsDictionary MethodsString Methods
    View all

Online Compilers

  • Python Compiler
  • Python Playground
  • Python Projects
  • Exercises

Tutorials

  • Learn Python
  • Python for Beginners
  • Variables and Types
  • Python Strings
  • Python Lists
  • Python If Else
  • Python For Loops
  • Python Functions
  • Python Classes

Examples

  • Python Examples
  • Hello World
  • Add Two Numbers
  • Even or Odd
  • Factorial
  • Fibonacci
  • Prime Check
  • Palindrome Check

Company

  • About
  • Contact
  • Privacy Policy
  • Terms of Use
  • Disclaimer

© 2026 ILoveCompiler. All rights reserved.

  1. Home
  2. Python Examples
  3. Star Pyramid Pattern

Pattern Programs · Python example

Star Pyramid Pattern

Use spaces and string repetition to print a centred star pyramid.

Open the compilerAll examples
beginnerpatternsloopsstrings

Pattern programs combine loops with string repetition. Each row needs the right number of leading spaces and stars.

example.py
rows = 5

for row in range(1, rows + 1):
    spaces = " " * (rows - row)
    stars = "*" * (2 * row - 1)
    print(spaces + stars)

How the code works

  • The loop runs once per row.
  • spaces centres the row by shrinking as row grows.
  • stars grows by two each iteration.

Key ideas

  • String multiplication repeats text: 'ab' * 3 is 'ababab'.
  • Row 1 has rows - 1 spaces and 1 star.
  • Each following row removes one space and adds two stars.

Try changing it

  • Print an inverted pyramid.
  • Build a diamond by adding a mirrored lower half.
  • Use a single loop with formatted width: print(f'{stars:^{2 * rows - 1}}').

Related examples

beginner

Floyd's Triangle

Print a right-angled triangle of consecutive natural numbers.

beginner

Multiplication Table

Print a ten-row multiplication table with a for loop and range().

Previous exampleQuadratic Equation RootsNext exampleFloyd's Triangle

Expected output

    *
   ***
  *****
 *******
*********

Output is compared after trimming trailing whitespace on each line. Run the code to see it produced by CPython in your browser.

Practice next

Apply the same idea with automatic tests or browse a guided project.

ExercisesProjects