Python 3 program to print a Triangle using star :
In this tutorial, we will learn how to print a triangle using star (_* ). You can change it to any other characters if you want. Printing a symbol works the same for any programming language. You can use the same logic on any other programming language like Java, R, C, C++ _etc. to get the same output.
I will also teach you later how to solve any problems like this one.
The program will take the height of the triangle as an input from the user. It will print out the triangle using star(_* _). You can customize this program to print the triangle using any other character.
Printing triangle using star :
#program 1
height = int(input("Enter the height of the triangle : "))
for i in range(height,0,-1):
print(i* ' ' + (height+1-i) * '*')
#program 2
height = int(input("Enter the height of the triangle : "))
for i in range(height,0,-1):
print(i* 'k' + (height+1-i) * '*')
You can also download the below programs from here.
The program to print triangle using star is as below :
Let’s check one example of how it will print :
We are using only one single print statement to write down the whole logic of the program. But how is it working? To understand the concept, let me do some modification to the program :
You can see the only thing we have changed is that we are printing out ‘k’ instead of space in the above program. It will result like below :
In the above pattern :
-
‘_k _’ is printed instead of a space ‘ ’. Think ‘k’ as space.
-
For the first line, ‘_k _’ is printed for ‘_height ’ times and ‘* _’ for 1 time. The height of the triangle is 5, so k is printed five times. In our original program, space was printed instead of ‘_k _‘.
-
For the second line, ‘_k ’ is printed for ‘height – 1’ times and ‘* _’ for 2 times. ‘_k _’ is printed four times and star is printed two times.
-
For the third line, ‘_k _’ is printed for ‘_height – 2 ’ times and ‘* _’ for 3 times. So, the number of times ‘_k _’ is printed is decreased by one on each step. For the first line, k is printed for height times or 5 times, for the second line, k is printed for 4 times, for the third time, it is printed for 3 times etc.
Similarly, the character or ’_* _’ is printed for one time in the first line, it is printed two times in the second line, three times in the third line etc.
We need only the height of the triangle. Based on its value, we can print it out using the above steps.
Now, let’s try to understand the program :
-
We have a ‘_for _’ loop that runs (height) times. ‘_i _’ is the value in this loop. ‘_i _’ will run from the value (height + 1) to 1. Each time it will be decremented by 1.
-
Inside the loop, first space ‘ ’ is printed for ‘_i ’ times and then ‘* _’ is printed for (_height + 1 -i _) times. Each line is printed on a new line.
That’s it. You can write down each step of the loop to understand the program better.
Conclusion :
In this tutorial, we have learned how to print a triangle using star in python. You can modify the program to print the triangle with any other character you want or you can also print the triangle using numbers or characters. Try to run the above example and drop one comment below if you have any queries.