Python Program to Print Hollow Box Pattern of Numbers

Write a Python Program to Print Hollow Box Pattern of Numbers 1 and 0 using For Loop and While Loop with an example.

Python Program to Print Hollow Box Pattern of Numbers 1 and 0 using For Loop

This Python program allows user to enter the total number of rows and columns. Next, we used Python Nested For Loop to iterate each row and column items. Within the loop, we used the If statement to check whether the row and column numbers are 1 or maximum. If True, Python print 1 otherwise, empty space.

# Python Program to Print Hollow Box Pattern of Numbers 1 and 0
 
rows = int(input("Please Enter the total Number of Rows  : "))
columns = int(input("Please Enter the total Number of Columns  : "))

print("Hollow Box Pattern of Numbers") 
 
for i in range(1, rows + 1):
    for j in range(1, columns + 1):
        if(i == 1 or i == rows or j == 1 or j == columns):          
            print('1', end = '  ')
        else:
            print(' ', end = '  ')
    print()
Python Program to Print Hollow Box Pattern of Numbers 1

Python Program to Display Hollow Box Pattern of Numbers 1 and 0 using While Loop

This Python program to display a hollow box pattern is the same as the above. However, we replaced the For Loop with While Loop

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

print("Hollow Box Pattern of Numbers") 
i = 1 
while(i <= rows):
    j = 1;
    while(j <= columns ):
        if(i == 1 or i == rows or j == 1 or j == columns):          
            print('1', 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  : 17
Hollow Box Pattern of Numbers
1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  
1                                               1  
1                                               1  
1                                               1  
1                                               1  
1                                               1  
1                                               1  
1                                               1  
1                                               1  
1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  
>>> 

Python Program to Display Hollow Box Pattern of Numbers 0 and 1

If you want to print a hollow box pattern of numbers 0 and 1, replace 1 in print statement with empty space and empty space with 1.

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

print("Hollow Box Pattern of Numbers") 
 
for i in range(1, rows + 1):
    for j in range(1, columns + 1):
        if(i == 1 or i == rows or j == 1 or j == columns):          
            print('0', end = '  ')
        else:
            print(' ', end = '  ')
    print()
Please Enter the total Number of Rows  : 12
Please Enter the total Number of Columns  : 15
Hollow Box Pattern of Numbers
0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  
0                                         0  
0                                         0  
0                                         0  
0                                         0  
0                                         0  
0                                         0  
0                                         0  
0                                         0  
0                                         0  
0                                         0  
0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  
>>>