0
0
PythonProgramBeginner · 2 min read

Python Program to Print Sandglass Star Pattern

You can print a sandglass star pattern in Python using nested for loops that first print decreasing stars and then increasing stars, like this: for i in range(n, 0, -1): print(' ' * (n - i) + '*' * (2 * i - 1)) for i in range(2, n + 1): print(' ' * (n - i) + '*' * (2 * i - 1)).
📋

Examples

Input3
Output***** *** * * *** *****
Input5
Output********* ******* ***** *** * * *** ***** ******* *********
Input1
Output*
🧠

How to Think About It

To print a sandglass star pattern, first print lines with stars decreasing from the maximum width to one star, adding spaces before stars to center them. Then print lines with stars increasing from one star back to the maximum width, again adding spaces to keep the shape symmetrical.
📐

Algorithm

1
Get input number n for the size of the sandglass.
2
For each line from n down to 1, print spaces followed by stars decreasing in count.
3
For each line from 2 up to n, print spaces followed by stars increasing in count.
4
Each line's stars count is calculated as (2 * current_line - 1) to keep the pattern symmetrical.
💻

Code

python
n = int(input())
for i in range(n, 0, -1):
    print(' ' * (n - i) + '*' * (2 * i - 1))
for i in range(2, n + 1):
    print(' ' * (n - i) + '*' * (2 * i - 1))
Output
***** *** * * *** *****
🔍

Dry Run

Let's trace input n=3 through the code

1

First loop i=3

Print 0 spaces and 5 stars: '*****'

2

First loop i=2

Print 1 space and 3 stars: ' ***'

3

First loop i=1

Print 2 spaces and 1 star: ' *'

4

Second loop i=2

Print 1 space and 3 stars: ' ***'

5

Second loop i=3

Print 0 spaces and 5 stars: '*****'

LoopiSpacesStarsPrinted Line
First loop305*****
First loop213 ***
First loop121 *
Second loop213 ***
Second loop305*****
💡

Why This Works

Step 1: Decreasing stars with spaces

The first loop prints lines with stars decreasing from the maximum count, adding spaces before stars to center the pattern.

Step 2: Increasing stars with spaces

The second loop prints lines with stars increasing from one star back to the maximum, again adding spaces to keep symmetry.

Step 3: Star count calculation

Each line's star count is calculated as 2 * i - 1 to maintain the sandglass shape with odd number of stars.

🔄

Alternative Approaches

Using while loops
python
n = int(input())
i = n
while i > 0:
    print(' ' * (n - i) + '*' * (2 * i - 1))
    i -= 1
i = 2
while i <= n:
    print(' ' * (n - i) + '*' * (2 * i - 1))
    i += 1
This uses while loops instead of for loops, which some beginners find easier to understand but is slightly longer.
Using a single loop with conditions
python
n = int(input())
for i in range(1, 2 * n):
    if i <= n:
        stars = 2 * (n - i + 1) - 1
        spaces = i - 1
    else:
        stars = 2 * (i - n) - 1
        spaces = 2 * n - i - 1
    print(' ' * spaces + '*' * stars)
This uses one loop and conditions to print both halves, making the code compact but a bit harder to read.

Complexity: O(n^2) time, O(1) space

Time Complexity

The program uses two loops each running up to n times, and each line prints up to 2n-1 characters, resulting in O(n^2) time.

Space Complexity

The program uses only a few variables and prints directly, so space complexity is O(1).

Which Approach is Fastest?

All approaches have similar time complexity; using for loops is more readable, while a single loop with conditions is more compact but less clear.

ApproachTimeSpaceBest For
For loops (main)O(n^2)O(1)Readability and simplicity
While loopsO(n^2)O(1)Beginners preferring while syntax
Single loop with conditionsO(n^2)O(1)Compact code, slightly harder to read
💡
Use spaces before stars to center the pattern and keep the sandglass shape symmetrical.
⚠️
Beginners often forget to adjust spaces, causing the pattern to be left-aligned instead of centered.