Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Python Program to Print an Inverted Star Pattern
In this article, we'll learn how to print inverted star patterns in Python. An inverted star pattern displays stars in descending order, where each row has fewer stars than the previous one. For example, with N=5 rows ?
***** **** *** ** *
Basic Inverted Star Pattern
The most common approach uses nested for loops − the outer loop controls rows, and the inner loop prints stars for each row.
Using Nested Loops
Here's how to create an inverted star pattern with spaces between stars ?
# Number of rows
N = 6
for i in range(N, 0, -1):
for j in range(i):
print("*", end=' ')
print() # New line after each row
* * * * * * * * * * * * * * * * * * * * *
Using Single Loop
You can also create the pattern using string multiplication in a single loop ?
n = 6
for i in range(n, 0, -1):
print('*' * i)
****** ***** **** *** ** *
Inverted Star Pyramid Pattern
An inverted pyramid pattern centers the stars and adds leading spaces to create a triangular shape that narrows downward.
Algorithm
- Start with N rows
- For each row i (from N down to 1):
- − Print (N-i) spaces for centering
- − Print (2*i-1) stars
- − Move to next line
Example
Here's the implementation using three loops ?
N = 5
for i in range(N, 0, -1):
# Print leading spaces
for j in range(N - i):
print(' ', end='')
# Print stars
for j in range(2 * i - 1):
print('*', end='')
print() # New line
*********
*******
*****
***
*
Comparison
| Pattern Type | Shape | Use Case |
|---|---|---|
| Basic Inverted | Left-aligned | Simple descending pattern |
| Inverted Pyramid | Centered | Decorative triangle shape |
Conclusion
Inverted star patterns use nested loops with descending ranges. Use basic patterns for simple displays and pyramid patterns for centered, decorative output.
