Python Program to Print Hollow Rectangle Star Pattern

Write a Python Program to print Hollow Rectangle Star Pattern using For Loop and While Loop with an example.

Python Program to Print Hollow Rectangle Star Pattern using For Loop

This Python program allows user to enter the total number of rows and columns required for a rectangle. Next, we used the Python Nested For Loop to iterate each row and column value. Within the For Loop, we used If Else statement: If the row or column element is either 0 or maximum – 1, then Python prints *; otherwise, empty space.

# Python Program to Print Hollow Rectangle Star Pattern

rows = int(input("Please Enter the Total Number of Rows  : "))
columns = int(input("Please Enter the Total Number of Columns  : "))

print("Hollow Rectangle Star Pattern") 
for i in range(rows):
    for j in range(columns):
        if(i == 0 or i == rows - 1 or j == 0 or j == columns - 1):
            print('*', end = '  ')
        else:
            print(' ', end = '  ')
    print()
Python Program to Print Hollow Rectangle Star Pattern 1

Python Program to Print Hollow Rectangle Stars Example 2

This Python program allows user to enter his/her own character. Next, it prints the hollow rectangle pattern of user-specified character.

# Python Program to Print Hollow Rectangle Star Pattern

rows = int(input("Please Enter the Total Number of Rows  : "))
columns = int(input("Please Enter the Total Number of Columns  : "))
ch = input("Please Enter any Character  : ")

print("Hollow Rectangle Star Pattern") 
for i in range(rows):
    for j in range(columns):
        if(i == 0 or i == rows - 1 or j == 0 or j == columns - 1):
            print('%c' %ch, end = '  ')
        else:
            print(' ', end = '  ')
    print()
Please Enter the Total Number of Rows  : 12
Please Enter the Total Number of Columns  : 20
Please Enter any Character  : #
Hollow Rectangle Star Pattern
#  #  #  #  #  #  #  #  #  #  #  #  #  #  #  #  #  #  #  #  
#                                                        #  
#                                                        #  
#                                                        #  
#                                                        #  
#                                                        #  
#                                                        #  
#                                                        #  
#                                                        #  
#                                                        #  
#                                                        #  
#  #  #  #  #  #  #  #  #  #  #  #  #  #  #  #  #  #  #  #  
>>> 

Python Program to Print Hollow Rectangle Stars using While Loop

This hollow rectangle of stars program is the same as the first example. However, we replaced the For Loop with While Loop

# Python Program to Print Hollow Rectangle Star Pattern

rows = int(input("Please Enter the Total Number of Rows  : "))
columns = int(input("Please Enter the Total Number of Columns  : "))

print("Hollow Rectangle Star Pattern") 
i = 0
while(i < rows):
    j = 0
    while(j < columns):
        if(i == 0 or i == rows - 1 or j == 0 or j == columns - 1):
            print('*', end = '  ')
        else:
            print(' ', end = '  ')
        j = j + 1
    i = i + 1
    print()
Please Enter the Total Number of Rows  : 10
Please Enter the Total Number of Columns  : 22
Hollow Rectangle Star Pattern
*  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  
*                                                              *  
*                                                              *  
*                                                              *  
*                                                              *  
*                                                              *  
*                                                              *  
*                                                              *  
*                                                              *  
*  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  
>>>