Inverted Center-Aligned Pyramid Star Pattern in Python

Beginner
⏱️ 9 min read
📚 Updated: Aug 2026
🎯 3 Code Examples
🚀 Live Preview
Outer loop reversed

What You’ll Learn

An inverted centered pyramid reuses Program 5’s formulas — (rows - i) spaces and (2 * i - 1) stars — but runs the outer loop from rows down to 1 so the widest row prints first. This tutorial covers reverse iteration, a live preview, algorithm steps, worked Python examples, edge cases, and complexity.

Shape Rule

Base on top

Widest odd star run first; tip star lands on the last line.

Countdown

i = rows..1

Reverse the outer loop — that is the only change from Program 5.

Same Inners

spaces + stars

rows - i spaces then 2*i - 1 stars — unchanged bodies.

Star Steps

9, 7, 5…

For five rows, printed star counts fall by two each line.

Live Preview

1–14 rows

Pick a height and draw the inverted pyramid instantly.

O(n²)

n² stars

Same totals as Program 5 — O(n²) time, O(1) extra space.

Introduction

An inverted center-aligned pyramid starts with the base row and narrows to a single tip star, with leading spaces so each shorter run stays centered.

It is the flip of Program 5: keep the same space and star formulas, reverse only the outer loop. That mirrors how Program 2 inverts Program 1, but with odd-width centering. The same body is the lower half of the filled diamond.

Why it matters?

Countdown outer loops are a classic interview tweak. Once you see that inverting a pyramid is “same inners, reverse i,” diamonds and stacked shapes become simple composition.

Key Highlights

Reverse Outer

i from rows down to 1.

Growing Margin

rows - i rises as i falls.

Shrinking Stars

2*i - 1 steps down by odds.

Diamond Half

Lower half of Program 10’s filled diamond.

In short: for i from rows down to 1, print rows - i spaces, then 2 * i - 1 stars, then a newline.

📝 Problem & Approach

Given a positive integer rows, print an inverted center-aligned pyramid of * characters with rows lines (widest first).

Python
# First 5 rows (spaces shown as ·)
# *********
# ·*******
# ··*****
# ···***
# ····*

Inputs & Outputs

ItemTypeDescription
rowsintPyramid height (typically ≥ 1). First line width is 2 * rows - 1.
Printed outputtextBase-to-tip rows: (rows - i) spaces + (2 * i - 1) stars with countdown i.

Minimal workflow

Pseudocode
for i from rows down to 1:
    for j from 1 to (rows - i):
        print " "
    for k from 1 to (2 * i - 1):
        print "*"
    print newline

Approach comparison

ApproachIdeaBest for
Countdown + two innersSame as Program 5; reverse outerLearning and interviews
"*" * shortcutBuild padding and stars as stringsShorter demos after formulas click

⚡ Quick Reference

GoalPattern
Walk rows base → tipfor i in range(rows, 0, -1):
Leading spacesfor j in range(1, rows - i + 1): print(" ", end="")
Odd star runfor k in range(1, 2 * i): print("*", end="")
First-line width2 * rows - 1 stars, 0 spaces
Flip uprightfor i in range(1, rows + 1): (see Program 5)
String shortcutprint(" " * (rows - i) + "*" * (2 * i - 1))

📋 Inverted Pyramid vs Upright vs Triangle

Same space/star formulas — outer-loop direction defines upright vs inverted.

Program 5
i = 1..rows

Upright pyramid — tip first

This page
i = rows..1

Inverted pyramid — base first

Program 2
i stars

Inverted left triangle — no centering

Program 10
+ upper

Filled diamond — this page as lower half

Context

When This Pattern Shows Up

Reach for an inverted pyramid when teaching countdown loops after the upright centered pyramid.

  1. After Program 5

    Natural “change one loop” follow-up once upright pyramids click.

  2. Countdown practice

    for i in range(n, 0, -1): is a staple interview warm-up.

  3. Diamond lower half

    Filled diamonds print this shape under the upright pyramid.

  4. Compare with Program 2

    Same “invert by reversing i” idea, with centering this time.

  5. Not a UI layout tool

    Console teaching pattern — not how you build app screens.

Key benefit: proves that flipping a centered pyramid is one outer-loop change — the gateway to stacking diamond halves.

🔮 Live Preview

Choose a height between 1 and 14 and draw the inverted centered pyramid in the browser.

Try 4, 5, or 7. First line width will be 2 * rows - 1.

Live result
Press "Draw pyramid".

Examples Gallery

Three complete Python programs — countdown nested loops, console input, and a "*" * shortcut. Click View Output to reveal sample console results.

📚 Getting Started

Print a five-row inverted centered pyramid with classic nested loops.

Example 1 — Fixed rows = 5

Outer loop counts down; space loop uses rows - i; star loop uses 2 * i - 1.

Python
rows = 5

for i in range(rows, 0, -1):
    for j in range(1, rows - i + 1):
        print(" ", end="")
    for k in range(1, 2 * i):
        print("*", end="")
    print()

How It Works

When i = 5, print 0 spaces and 9 stars. When i = 1, print 4 spaces and 1 star. Star counts per printed line: 9, 7, 5, 3, 1.

📈 Practical Variant

Let the user choose the height at runtime.

Example 2 — User Input Version

Read rows with input() and int() (wrap in try/except ValueError in real apps).

Python
rows = int(input("Enter the number of rows: "))

for i in range(rows, 0, -1):
    for j in range(1, rows - i + 1):
        print(" ", end="")
    for k in range(1, 2 * i):
        print("*", end="")
    print()

How It Works

Same countdown space/star core as Example 1; only the source of rows changes. Non-numeric input raises ValueError with bare int(input()) — use try/except for safer labs.

⚡ Shortcut Style

Same inverted pyramid without explicit character loops.

Example 3 — " " * and "*" * Multiplication

Build each row’s margin and odd star run in one call each, still counting down.

Python
rows = 5

for i in range(rows, 0, -1):
    print(" " * (rows - i) + "*" * (2 * i - 1))

How It Works

Same formulas as Example 1; "*" * replaces the two inner loops. Keep the nested-loop version for exams that want both bounds visible.

🧠 How the Algorithm Prints Rows

1

Set up

Set rows. Use i counting down, j for spaces, k for stars.

Setup
2

Outer loop (reverse)

for i in range(rows, 0, -1): — base when i == rows, tip when i == 1.

Direction
3

Growing spaces

for j in range(1, rows - i + 1): print(" ", end="") — margin grows as i shrinks.

Center
4

Shrinking stars then newline

for k in range(1, 2 * i): print("*", end="") then print().

Stars
=

Inverted pyramid

Total stars still ; O(n²) time, O(1) extra space. First line width 2n - 1.

🔎 Worked Walkthrough — rows = 4

Trace spaces, stars, and characters per row as i counts down from 4 to 1.

iSpaces rows - iStars 2*i - 1Chars before newlinePrinted row
4077*******
3156 *****
2235  ***
1314   *

Star total: 7+5+3+1 = 16 = 4² — same as Program 5, different print order.

Use Cases

Where this inverted pyramid (and countdown centering) shows up beyond the homework prompt.

1. Teaching Loop Direction

One formula pair, two shapes — upright vs inverted.

Example: flip Program 5’s outer loop only.

2. Diamond Lower Half

Stack under Program 5 (often from rows - 1).

Example: Program 10.

3. Flip Back Upright

Change to i = 1..rows to restore Program 5.

Example: Program 5.

4. Compare With Program 2

Same invert idea; Program 2 has no leading spaces.

Example: side-by-side for rows = 5.

5. Hollow Variants

Once solid works, print border stars only.

Example: outline of each odd run.

6. Input Validation Labs

Pair with input validation and positive-row checks.

Example: reject rows <= 0 and re-prompt.

Pro Tip: say “Program 5 inners, countdown outer” before coding — that is the whole design.

Advantages

Why the inverted pyramid is a favorite follow-up pattern.

  1. 1. Minimal Diff From Program 5

    Reuse known space/star formulas; only reverse i.

  2. 2. Clear Countdown Feedback

    Wrong direction instantly prints an upright pyramid instead.

  3. 3. Completes Diamond Halves

    Pair with Program 5 for filled diamonds.

  4. 4. Same Complexity Story

    Total stars still n² — order does not change big-O.

Pro Tip: master the nested-loop countdown first; treat "*" * as a polish shortcut afterward.

Usage Tips

Small habits that keep inverted-pyramid code clean.

  1. 1. Start at rows, Step Down

    i++ by mistake reprints Program 5.

  2. 2. Keep 2 * i - 1

    Even widths break the classic single-peak tip.

  3. 3. Wrap int(input()) in try/except

    Avoid crashes when the user types letters instead of a number.

  4. 4. Use Real Spaces, Not Tabs

    Tabs break centering across fonts and editors.

  5. 5. Dry-Run One Small n

    Trace rows = 4 countdown on paper before larger demos.

Pro Tip: if the tip prints first, you almost certainly used i++ instead of i--.

Common Pitfalls

Mistakes that commonly break inverted pyramids.

  1. 1. Incrementing Instead of Decrementing

    for i in range(1, rows + 1): reprints the upright pyramid.

    → Use for i in range(rows, 0, -1):.

  2. 2. Using i Stars Instead of 2*i - 1

    You lose the centered odd-width shape.

    → Keep odd counts: 2 * i - 1.

  3. 3. Off-by-One on Spaces

    j < rows - i instead of <= shifts the tip off-center.

    → Use j <= rows - i.

  4. 4. Mixing Tabs With Spaces

    Centering looks fine in one editor and broken in another.

    → Always print the space character " ".

  5. 5. Blind int(input())

    Letters or empty input throw ValueError.

    → Catch ValueError and re-prompt on failure.

Edge Cases

Check these inputs before calling the solution done.

rows = 1

Single star

One iteration: 0 spaces + 1 star — tip and base coincide.

rows = 0

Empty pattern

Outer loop never runs — print nothing or show a message.

Negative

rows < 0

Treat as invalid; re-prompt instead of silent empty output.

Large n

Wide first line

Top width 2n-1 — fine for labs; may wrap on tiny terminals.

Bad input

Non-numeric ReadLine

int(input()) raises ValueError — validate first.

First row

i == rows

Space loop runs 0 times; print 2*rows-1 stars only.

🎯 Practice Problems

Try these variations to lock in the pattern.

1. Flip back to Program 5

  • Change outer loop to i = 1..rows
  • Confirm upright pyramid returns

2. Drop centering

  • Remove the space loop; keep countdown + 2*i-1
  • Compare with left-aligned odd-width wedges

3. Verify star total

  • Count printed stars; assert equals rows * rows
  • Same check as Program 5

4. Build a diamond

  • Print Program 5, then this body from rows - 1
  • Match Program 10

Notes

  • Same totals as Program 5. Order changes; star count is still n².
  • Per-row character count before newline is still rows + i - 1 — tip rows are shorter than the top base.
  • Validate rows > 0 for interactive programs; rows = 1 prints a single star.
  • Next: Program 7 moves to a hollow inverted-V outline pattern.

Quick Takeaway: countdown i from rows to 1, print rows - i spaces and 2 * i - 1 stars — that is the inverted pyramid.

⏱️ Time and Space Complexity

ProgramTimeExtra space
Nested space/star loops (Examples 1–2)O(rows²)O(1)
"*" * shortcut (Example 3)O(rows²)O(rows) temporary per row string

Total stars = rows²; each row also prints up to Θ(rows) spaces. Same as Program 5.

Wrap Up

🎉 Conclusion

The inverted centered pyramid is Program 5 with a countdown outer loop: rows - i spaces and 2 * i - 1 stars, printed from base to tip. Master that flip and diamond halves become a short stacking exercise.

Practice the three examples above, then continue to the hollow inverted-V pattern.

Spaces grow, odd stars shrink, total stars = n² — keep i--, and validate row counts when reading input.

💡 Best Practices

✅ Do

  • Explain “Program 5 inners + countdown outer” before coding
  • Keep odd star counts for a single-peak tip
  • Print real space characters, not tabs
  • State that total stars equal n² (same as Program 5)
  • Use try/except ValueError for interactive demos

❌ Don’t

  • Increment i when you meant an inverted pyramid
  • Use 2 * i even widths for the classic shape
  • Skip leading spaces and expect centering
  • Ignore bad console input in user-facing demos
  • Skip the rows = 1 edge case

Key Takeaways

Knowledge Unlocked

Five things to remember about the inverted pyramid

Print the upside-down pyramid the beginner-friendly way.

5
Core concepts
02

Outer

i = rows..1

Direction
* 03

Inners

Same as Prog 5

Formula
n 04

Total

n² stars

Math
O 05

Complexity

O(n²) time

Analysis

❓ Frequently Asked Questions

The outer loop runs i from rows down to 1. The star count is still 2*i-1, so when i is large you print many stars first; as i shrinks, stars become 9,7,5,… and spaces (rows-i) grow from 0 upward. Same formulas as Program 5, only the order of i values is reversed.
Spaces use (rows-i). When i is rows, rows-i is 0; when i is 1, rows-i is rows-1. So as i steps down each printed line, the margin grows while 2*i-1 shrinks, which keeps the narrower rows centered under the wide top row.
Program 5 uses for i in range(1, rows + 1) so stars grow each line. Program 6 uses for i in range(rows, 0, -1) with the same inner loops, so the first printed line is the base and the last line is the tip.
When i starts at rows, you print 0 spaces and 2 * rows - 1 stars — the widest line of the inverted pyramid.
O(n²) for n rows. Same totals as Program 5; only iteration order differs. Total stars equal n².
Program 2 is an inverted left-aligned triangle (i stars, no centering). Program 6 keeps (rows-i) spaces and odd star runs so the tip stays centered.
Yes. With the countdown outer loop: print(" " * (rows - i) + "*" * (2 * i - 1)).
Wrap int(input()) in try/except ValueError so bad input does not crash the script.

Did you Know? 🔊

This inverted pyramid is exactly Program 5 with the outer loop reversed — the same relationship as Program 1 versus Program 2, but with centered odd-width rows. Total stars still equal ; only print order changes.

Continue to Inverted V Hollow

Next up: a hollow inverted-V outline that becomes the upper half of a hollow diamond.

Program 7 tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

12 people found this page helpful