Right-Angled Triangle Star Pattern in Python

What You'll Learn
This is the first star pattern in the series. For each row, print one more * than the previous row. In Python, this is easy with either nested loops or string repetition.
Total stars printed for rows = n is \(1 + 2 + \cdots + n = \frac{n(n+1)}{2}\).
⭐ 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):
for _ in range(i):
print("*", end="")
print()🧠 How It Works
Setup
rows fixes the triangle height. for i in range(1, rows + 1): walks row indices 1 through rows (each i is one output line).
Stars on one line: end=""
for _ in range(i): runs i times. Each time, print("*", end="") appends a star without a newline so all stars for that row sit on the same line.
New line
A bare print() after the inner loop emits only a newline, finishing the row before the next i. (The variation can use print("*" * i) instead—same shape.)
Right-angled triangle
Total stars: 1+2+…+n = n(n+1)/2 — O(n²) output for n = rows, O(1) extra memory. Long rows scroll horizontally 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("*" * i)💡 Tips for Enhancement
Try These
- Use
"*" * ifor a shorter solution (shown in Example 2) - Validate input (reject
rows < 1) - Print the triangle with spaces between stars (e.g.,
"* " * i) - Store each row in a list and join at the end for testing
- Try the inverted triangle next (Program 2)
Avoid
- Printing a trailing space after each row (unless you want it)
- Forgetting
print()at the end of each row in the nested-loop version - Using string concatenation in a loop for very large rows (prefer repetition)
- Hardcoding input values when writing reusable code
- Assuming user input is always valid
Key Takeaways
Row i prints exactly i stars.
Total stars for n rows is \(n(n+1)/2\).
Python can print each row quickly using "*" * i.
Time complexity is O(n²) because total printed characters grow quadratically.
This is a great starter pattern to learn nested loops.
❓ Frequently Asked Questions
"*" * i is concise and readable."* " * i instead of "*" * i. If you want to remove the last space, use ("* " * i).rstrip().n rows, because total printed characters are proportional to \(n(n+1)/2\).Next: Inverted Right-Angled Triangle
Continue to Program 2 to print the inverted version of this triangle.
In Python, repeating strings (like "*" * i) is a common and fast way to generate pattern rows.
10 people found this page helpful
