Inverted V-Shaped Hollow Star Pattern in Python

What You'll Learn
This program prints a hollow inverted V (one star on top, then two stars farther apart each row). See Program 8 for the upright V (wide row first, vertex at the bottom).
Each row starts with spaces to shift the left edge, then one star; from row 2 onward, an inner gap of 2 * i - 3 (growing by 2 each row) and a second star.
⭐ Pattern Output
When you run the program with rows = 5:
*
* *
* *
* *
* *Complete Python Program
Fixed rows = 5 version:
rows = 5
for i in range(1, rows + 1):
print(" " * (rows - i), end="")
print("*", end="")
if i > 1:
print(" " * (2 * i - 3), end="")
print("*", end="")
print()🧠 How It Works
Setup
rows sets the height. for i in range(1, rows + 1): walks from the apex to the wide base. Each row is built with print(..., end="") pieces, then print() finishes the line.
Left margin and first star
print(" " * (rows - i), end="") shifts the row right. print("*", end="") draws the left leg of the V.
Gap and right star (when i > 1)
if i > 1: then print(" " * (2 * i - 3), end="") hollows the interior (gap width grows by 2 per row), and print("*", end="") places the right leg. Row 1 skips this block so you get a single apex star.
Finish the line
print() adds the newline. Each row has width 2 * rows - 1 characters (padding + stars + interior spaces).
Hollow inverted V
O(n²) output for n = rows, O(1) extra space. Width 2n - 1 scrolls inside the green preview on phones.
Variation — User Input Version
Read rows from user input:
rows = int(input("Enter the number of rows: "))
for i in range(1, rows + 1):
print(" " * (rows - i), end="")
print("*", end="")
if i > 1:
print(" " * (2 * i - 3), end="")
print("*", end="")
print()💡 Tips for Enhancement
Try These
- Validate input (reject
rows < 1) - Print the V-shaped hollow half next (Program 8) to form a diamond (Program 9)
- Replace
*with other characters like# - Use string-building to create each row, then print once per line
- Print a filled V by printing stars instead of spaces in the inner gap
Avoid
- Printing the second star on the first row (it duplicates the tip)
- Mixing tabs and spaces for alignment
- Forgetting newlines between rows
- Using the wrong gap formula (it should be
2*i - 3) - Assuming user input is always valid
Key Takeaways
Each row prints one left edge star and (except the first row) one right edge star.
Leading spaces are rows - i to position the left edge.
Inner gap is 2 * i - 3, growing by 2 each row.
Total printed characters per row are \(\Theta(rows)\), so total time is O(rows²).
This pattern is the upper half of a hollow diamond (Program 9).
❓ Frequently Asked Questions
n rows: there are \(n\) lines and each line prints \(\Theta(n)\) characters.Next: V-Shaped Hollow
Continue to Program 8 to print the upright V-shaped hollow pattern and complete a hollow diamond in Program 9.
This pattern is a building block for hollow diamonds: add the V-shaped half (Program 8) below it to form Program 9.
9 people found this page helpful
