Python Program to Print a Hollow Triangle Pattern
Beginner · Patterns
A hollow triangle pattern prints just the three edges of a triangle and leaves the inside empty. This program draws a right-angled hollow triangle, with stars down the first column, along the slanted edge and across the bottom row, and spaces everywhere else.
Program
* * * * * * * * * * * *
How it works
- Row
ihasipositions, numberedjfrom 1 toi, just like a solid right-angled triangle. j == 1is the left edge,j == iis the last position of the row, which forms the slanted edge, andi == rowsis the whole bottom row.- Every other position is inside the triangle, so it prints a space instead of a star.
- Each character is followed by a space with
end=" ", which spreads the stars out so the edges are easy to see.
Other ways to write it
Hollow pyramid
* * * * * * * *********
A hollow pyramid keeps the outline of a normal pyramid: one star at the tip, a full row at the base, and two stars with a growing gap on every row in between.
Hollow inverted triangle
* * * * * * * * * * * *
Counting the rows down instead of up flips the triangle, so the full row of stars is now at the top.
Try it yourself
Change the condition so the bottom row is left out, leaving only two edges of the triangle.
Edit the program above and press Run, or open it in the full editor to save it and share it. To watch it run line by line, open it in the visualizer.
Related programs
All Python programs · CBSE class 12 practical file · Python exercises