Pythondex
Python Pattern Programs

Python Program To Print Hollow Right Angled Triangle Pattern

Last updated May 1, 2023 by Jarvis Silva

Want to know how to print hollow right angled triangle pattern using python, then you are at the right place today in this tutorial we will create a python program to print hollow right angled triangle pattern.


row = 10

for i in range(row):
    for j in range(i+1):
        if j==0 or j==i or i==row-1:
            print('*',end=" ")
        else:
            print(" ", end=" ")
    print()

Above is the code to print hollow right angled triangle pattern in python, this code uses nested for loops to print a hollow right angled triangle pattern. Here’s a step-by-step explanation of what happens:

  • The first line of the code sets the value of row to 10. This variable determines the height of the pattern.
  • The for i in range(row): loop iterates over the range of numbers from 0 to row-1, inclusive. This outer loop controls the number of rows in the pattern.
  • Inside the outer loop, we have another loop: for j in range(i+1):. This inner loop iterates over the range of numbers from 0 to i, inclusive. This controls the number of columns in each row of the pattern.
  • The if j==0 or j==i or i==row-1: statement checks whether the current column is on the left edge, right edge, or bottom row of the pattern. If any of these conditions are true, we print an asterisk (*) to the console using print('*',end=" "). Otherwise, we print a space character using print(" ", end=" ").
  • The print() statement at the end of the outer loop moves the cursor to a new line, so the next row of the pattern is printed on a new line.

To run this program you need to have python installed on your computer, If you don’t have then follow this guide: Install and setup python on your computer or you can use this online python compiler.


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

After running this program you will see a hollow right angled triangle pattern printed like above, I hope you found this tutorial helpful and useful. Do share this tutorial with your friends who might be interested in this program.

Here are some more python drawing tutorials for you:

I hope you found what you were looking for from this tutorial, and if you want more python programs and tutorials like this, do join our Telegram channel for future updates.

Thanks for reading, have a nice day 🙂