Right-Angled Triangle Star Pattern in Python

Beginner
⏱️ 8 min read
📚 Updated: Aug 2025
🎯 2 Code Examples
n(n+1)/2 stars total

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:

Output
*
**
***
****
*****
1

Complete Python Program

Fixed rows = 5 version:

Python
rows = 5

for i in range(1, rows + 1):
    for _ in range(i):
        print("*", end="")
    print()

🧠 How It Works

1

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).

Setup
2

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.

Inner loop
3

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.)

Line break
=

Right-angled triangle

Total stars: 1+2+…+n = n(n+1)/2O(n²) output for n = rows, O(1) extra memory. Long rows scroll horizontally inside the green preview on phones.

2

Variation — User Input Version

Read rows from user input:

Python
rows = int(input("Enter the number of rows: "))

for i in range(1, rows + 1):
    print("*" * i)

💡 Tips for Enhancement

Try These

  • Use "*" * i for 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

1

Row i prints exactly i stars.

2

Total stars for n rows is \(n(n+1)/2\).

3

Python can print each row quickly using "*" * i.

4

Time complexity is O(n²) because total printed characters grow quadratically.

5

This is a great starter pattern to learn nested loops.

❓ Frequently Asked Questions

For learning, nested loops are perfect. For clean Python code, "*" * i is concise and readable.
Print "* " * i instead of "*" * i. If you want to remove the last space, use ("* " * i).rstrip().
It’s O(n²) for 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.

Program 2 →
Did you know?

In Python, repeating strings (like "*" * i) is a common and fast way to generate pattern rows.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

10 people found this page helpful