How to print a star triangle using Python?

You can use loop statements and string concatenation to output a triangle of asterisks. Here is an example code:

rows = 5

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

The output result is:

*
**
***
****
*****

You can modify the value of “rows” to output star triangles of different sizes as needed.

bannerAds