⚡ TL;DR
Print a left-aligned star triangle in Python using nested loops. Includes code walkthrough, output, and 4 variations to practice loop logic.
The left triangle (right-aligned) star pattern is the mirror image of the right triangle. It’s a common interview warm-up and a great exercise for understanding loop indexing.
Target Pattern
*
**
***
****
*****Each row has n - i leading spaces followed by i stars.
Approach 1: Nested Loops with Spaces
def left_triangle(n):
for i in range(1, n + 1):
# Print leading spaces
for j in range(n - i):
print(" ", end="")
# Print stars
for k in range(i):
print("*", end="")
print()
left_triangle(5)Output
*
**
***
****
*****Approach 2: String Multiplication (More Pythonic)
def left_triangle_pythonic(n):
for i in range(1, n + 1):
print(" " * (n - i) + "*" * i)
left_triangle_pythonic(5)Output
*
**
***
****
*****Approach 3: Using rjust
def left_triangle_rjust(n):
for i in range(1, n + 1):
print(("*" * i).rjust(n))
left_triangle_rjust(5)Output
*
**
***
****
*****Approach 4: Right Triangle with Numbers
def left_triangle_numbers(n):
for i in range(1, n + 1):
spaces = " " * (n - i)
nums = " ".join(str(j) for j in range(1, i + 1))
print(spaces + nums)
left_triangle_numbers(5)Output
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5Comparison Table
| Approach | Lines of Code | Readability | Pythonic? |
|---|---|---|---|
| Nested Loops | 6 | ★★★★★ | ★★★☆☆ |
| String Multiplication | 3 | ★★★★★ | ★★★★★ |
| rjust() | 3 | ★★★★☆ | ★★★★☆ |
| With Numbers | 5 | ★★★★☆ | ★★★☆☆ |
Complexity Analysis
- Time Complexity: O(n²) — total iterations = 1+2+…+n = n(n+1)/2
- Space Complexity: O(1) — no extra storage
Practice Variations
- Left triangle with letters — replace ”*” with letter of the alphabet
- Hollow left triangle — print stars only on the border
- Inverted left triangle — flip the row order
- Left triangle with different characters —
*,#,+
Real-World Use Cases
- Console output formatting (progress bars, tables)
- ASCII art generation
- Image processing preview mockups
- Bootcamp assignments / coding interviews
Try these exercises to solidify your Python loop logic! Next: Inverted Right Triangle Pattern in Python
Related Patterns
FAQ
How do you print the Left Triangle Star pattern in Python?
Use nested loops: the outer loop walks through the rows while the inner loop prints the characters or values for each row. The complete Python implementation with expected output is shown in the sections above.
What is the time complexity of the Left Triangle Star pattern in Python?
The time complexity is O(n²) and the space complexity is O(1), since the pattern is built with a fixed number of loop counters and printed row by row.
