Python program to print a triangle using star
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.
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.
Similar tutorials :
- Python 3 program to print invert right angle triangle
- Python program to print a right angle triangle
- Python program to find out the perimeter of a triangle
- Python program to find the area and perimeter of a triangle
- Python program to print a star hollow square pattern
- Python program to print list elements in different ways