Python Program to Print Left Triangle Star Pattern
Use a
for loop with range(1, n+1) and print stars using print('*' * i) to create a left triangle star pattern in Python.Examples
Input3
Output*
**
***
Input5
Output*
**
***
****
*****
Input1
Output*
How to Think About It
To print a left triangle star pattern, think of printing one star on the first line, two stars on the second, and so on until the nth line. Use a loop that counts from 1 to n, and on each iteration, print that many stars.
Algorithm
1
Get the number of rows (n) from the user.2
Start a loop from 1 to n (inclusive).3
On each iteration, print stars equal to the current loop number.4
Move to the next line after printing stars.5
End the loop after n lines.Code
python
n = int(input('Enter number of rows: ')) for i in range(1, n + 1): print('*' * i)
Output
Enter number of rows: 5
*
**
***
****
*****
Dry Run
Let's trace the program with input n=3 to see how stars are printed line by line.
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: ***
| Iteration (i) | Stars Printed |
|---|---|
| 1 | * |
| 2 | ** |
| 3 | *** |
Why This Works
Step 1: Loop controls the number of lines
The for loop runs from 1 to n, so it controls how many lines are printed.
Step 2: Stars printed increase each line
On each line, '*' * i prints stars equal to the current line number i.
Step 3: Print moves to next line automatically
The print function adds a new line after printing stars, so each line appears below the previous.
Alternative Approaches
Using nested loops
python
n = int(input('Enter number of rows: ')) for i in range(1, n + 1): for j in range(i): print('*', end='') print()
This uses two loops: outer for lines, inner for stars; slightly longer but shows nested loop concept.
Using while loop
python
n = int(input('Enter number of rows: ')) i = 1 while i <= n: print('*' * i) i += 1
Uses a while loop instead of for loop; good for understanding different loop types.
Complexity: O(n^2) time, O(1) space
Time Complexity
The outer loop runs n times, and printing stars takes up to n operations on the last line, so total is roughly O(n^2).
Space Complexity
No extra space is used except loop variables, so space complexity is O(1).
Which Approach is Fastest?
Using '*' * i is concise and efficient; nested loops are more verbose but conceptually clear.
| Approach | Time | Space | Best For |
|---|---|---|---|
| Single loop with string multiplication | O(n^2) | O(1) | Simple and concise code |
| Nested loops | O(n^2) | O(1) | Learning nested loops concept |
| While loop | O(n^2) | O(1) | Understanding different loop types |
Use
'*' * i to quickly print repeated stars without inner loops.Beginners often forget to increase the loop counter or print stars on the same line without moving to the next.