Print Hollow Triangle Pattern in Python

In this article, we will learn how to print a hollow triangle pattern using Python.

Example

Input: n = 5
Output:
**********
****  ****
***    ***
**      **
*        *

Hollow Triangle Pattern in Python

1. Get the number of row n from the user.

2. Declare a local variable s to keep track of the number of spaces need to print.

3. Iterate through n as an outer loop till the number of rows is greater than 1.

4. Iterate through from range i to s+1 as an inner loop, print space.

5. After printing all columns of rows, print in a new line.

def hollow_pattern(n):
    s = 1 # for number of spaces
    for i in range(n, 0, -1):
        for j in range(1, i+1):
            print('*', end=' ')

        if i != n:
            # to print space
            for k in range(1, s+1):
                print(' ', end=' ')
            s += 2
        for j in range(i, 0, -1):
            if j!= n:
                print('*', end=' ')
        print()
    print()
n = int(input("Enter number of row: "))
hollow_pattern(n)

Output

Enter number of row: 10
* * * * * * * * * * * * * * * * * * * 
* * * * * * * * *   * * * * * * * * *
* * * * * * * *       * * * * * * * *
* * * * * * *           * * * * * * *
* * * * * *               * * * * * *
* * * * *                   * * * * *
* * * *                       * * * *
* * *                           * * *
* *                               * *
*                                   *

Also, refer

Leave a Reply

Your email address will not be published.