0
0
PythonProgramBeginner · 2 min read

Python Program to Print Right Triangle Star Pattern

Use a nested loop in Python where the outer loop runs from 1 to n and the inner loop prints stars * equal to the current outer loop count; for example, for i in range(1, n+1): print('*' * i) prints a right triangle star pattern.
📋

Examples

Input3
Output* ** ***
Input5
Output* ** *** **** *****
Input1
Output*
🧠

How to Think About It

To print a right triangle star pattern, think of each line as having a number of stars equal to the line number. Start from line 1 with one star, then line 2 with two stars, and so on until the last line has n stars. Use a loop to control the lines and another to print stars on each line.
📐

Algorithm

1
Get the number of lines n from the user.
2
For each line number i from 1 to n:
3
Print i stars on that line.
4
Move to the next line until all lines are printed.
💻

Code

python
n = int(input("Enter the number of lines: "))
for i in range(1, n + 1):
    print('*' * i)
Output
* ** *** **** *****
🔍

Dry Run

Let's trace the program with input n=3 to see how it prints the star pattern.

1

Input

User inputs n = 3

2

First iteration (i=1)

Print '*' repeated 1 time: *

3

Second iteration (i=2)

Print '*' repeated 2 times: **

4

Third iteration (i=3)

Print '*' repeated 3 times: ***

iPrinted Stars
1*
2**
3***
💡

Why This Works

Step 1: Outer loop controls lines

The outer loop runs from 1 to n, each number representing the current line number.

Step 2: Print stars equal to line number

For each line i, printing '*' repeated i times creates the right triangle shape.

Step 3: Moving to next line

After printing stars for one line, the program moves to the next line until all lines are printed.

🔄

Alternative Approaches

Using nested loops
python
n = int(input("Enter number of lines: "))
for i in range(1, n + 1):
    for j in range(i):
        print('*', end='')
    print()
This method uses an inner loop to print stars one by one, which is more explicit but slightly longer.
Using list comprehension and join
python
n = int(input("Enter number of lines: "))
for i in range(1, n + 1):
    print(''.join(['*' for _ in range(i)]))
This uses list comprehension to build the star string, which is more flexible but less concise.

Complexity: O(n^2) time, O(1) space

Time Complexity

The outer loop runs n times and the inner printing of stars takes up to n steps in the last line, resulting in O(n^2) time.

Space Complexity

The program uses constant extra space, only storing loop counters and printing directly.

Which Approach is Fastest?

Using string multiplication is faster and simpler than nested loops because it avoids inner loop overhead.

ApproachTimeSpaceBest For
String multiplicationO(n^2)O(1)Simple and fast printing
Nested loopsO(n^2)O(1)Explicit control over printing
List comprehensionO(n^2)O(1)Flexible string building
💡
Use string multiplication like '*' * i to print repeated stars efficiently.
⚠️
Beginners often forget to increase the number of stars each line, printing the same number every time.