V-Shaped Hollow Star Pattern in Python

What You'll Learn
This program prints a hollow V (wide first row, legs meet at the bottom). See Program 7 for the inverted V. It uses for i in range(rows, 0, -1) so the inner gap shrinks by 2 each row.
For each row, print leading spaces, a left edge star, an optional inner gap, and a right edge star.
⭐ Pattern Output
When you run the program with rows = 5:
* *
* *
* *
* *
*Complete Python Program
Fixed rows = 5 version:
rows = 5
for i in range(rows, 0, -1):
print(" " * (rows - i), end="")
print("*", end="")
if i > 1:
print(" " * (2 * i - 3), end="")
print("*", end="")
print()🧠 How It Works
Setup
for i in range(rows, 0, -1): prints the wide pair of legs first and ends at the bottom vertex. The same print(..., end="") pattern as Program 7 applies; only i’s direction changes.
Left margin and first star
print(" " * (rows - i), end="") then print("*", end=""). As i counts down, rows - i grows, shifting the whole row rightward each time.
Gap and second star
if i > 1: prints " " * (2 * i - 3) then the second *. When i == 1, only the first star and margin appear—the bottom vertex.
Finish the line
print() ends each row. Line width stays 2 * rows - 1, same geometry as Program 7 with reversed row order.
Hollow V
Vertex at the bottom, opening upward. O(n²) output for n = rows, O(1) extra space. Wide rows scroll horizontally in the green preview on small screens.
Variation — User Input Version
Read rows from user input:
rows = int(input("Enter the number of rows: "))
for i in range(rows, 0, -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) - Combine with Program 7 to form a hollow diamond (Program 9)
- Replace
*with other characters like# - Build each row as a string and print once per line
- Try printing a filled V by replacing the gap with stars
Avoid
- Printing the second star on the last 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
The pattern is the mirror of Program 7: start wide and shrink toward the bottom vertex.
Leading spaces are rows - i.
Inner gap is 2 * i - 3 and decreases by 2 each row.
Time complexity is O(rows²) because each row prints \(\Theta(rows)\) characters.
This pattern forms the lower half of a hollow diamond (Program 9).
❓ Frequently Asked Questions
rows - 1 to avoid duplicating the middle row. That creates Program 9.n rows: there are \(n\) lines and each line prints \(\Theta(n)\) characters.Next: Hollow Diamond Pattern
Continue to Program 9 to combine Program 7 and Program 8 into a hollow diamond.
Starting the lower half from rows - 1 is a common trick to avoid printing the center row twice when building symmetric patterns.
9 people found this page helpful
