⚡ TL;DR
Learn how to print a right triangle star pattern in Python with for-loops. Includes full code, output walkthrough, and 5 variations for beginners.
The right triangle star pattern is the most common first pattern in programming tutorials. It teaches you nested loops, printing without newline, and how to control row-by-row output.
What We’ll Build
*
* *
* * *
* * * *
* * * * *Basic Approach: Nested For Loops
The outer loop controls the row, inner loop controls how many stars per row.
def right_triangle(n):
for i in range(1, n + 1): # rows
for j in range(i): # stars per row
print("*", end=" ")
print() # new line after each row
# Test with 5 rows
right_triangle(5)Output
*
* *
* * *
* * * *
* * * * * Variation 1: Using end and sep
Python 3 lets you control line endings directly.
def right_triangle_v2(n):
for i in range(1, n + 1):
print("* " * i, end="")
print() # newline
right_triangle_v2(5)Output
*
* *
* * *
* * * *
* * * * *Variation 2: Using str.join()
def right_triangle_v3(n):
for i in range(1, n + 1):
print(" ".join(["*"] * i))
right_triangle_v3(5)Output
*
* *
* * *
* * * *
* * * * *Variation 3: With Numbers Instead of Stars
def right_triangle_numbers(n):
for i in range(1, n + 1):
print(" ".join(str(j) for j in range(1, i + 1)))
right_triangle_numbers(5)Output
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5Variation 4: Right-Aligned (Left Triangle)
def left_triangle(n):
for i in range(1, n + 1):
print(" " * (n - i) + "* " * i, end="")
print()
left_triangle(5)Output
*
* *
* * *
* * * *
* * * * * Common Beginner Errors
- Forgetting
end=""→ every star prints on its own line - Off-by-one in range → Python’s
range(1, n+1)is exclusive, so you get exactly n rows - Mixing tabs and spaces → inconsistent alignment
Time & Space Complexity
| Metric | Value |
|---|---|
| Time | O(n²) |
| Space | O(1) (no extra storage) |
Practice Exercises
- Change the star count formula: try
print("* " * (2*i - 1))to get odd columns - Print row numbers instead of stars:
1,2 2,3 3 3… - Print reversed rows:
5 4 3 2 1,4 3 2 1, etc.
Try these variations in your own Python REPL or script. Next up: Inverted Right Triangle Pattern in Python
Related Patterns
FAQ
How do you print the Right 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 Right 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.
