Python Program to Print Hollow Square Star With Diagonals

Write a Python Program to Print Hollow Square Star With Diagonals using a for loop. The if condition checks whether the position is Diagonal or the outer line, and if true, print stars; otherwise, print a space.

# Python Program to Print Hollow Square Star With Diagonals Pattern
 
rows = int(input("Enter Hollow Square Star With Diagonals Rows = "))

print("Hollow Square Star With Diagonals Pattern") 

for i in range(rows):
    for j in range(rows):
        if(i == 0 or i == rows - 1 or j == 0 or j == rows - 1
           or i == j or j == (rows - 1 - i)):
            print('* ', end = '')
        else:
            print('  ', end = '')
    print()
Enter Hollow Square Star With Diagonals Rows = 12
Hollow Square Star With Diagonals Pattern
* * * * * * * * * * * * 
* *                 * * 
*   *             *   * 
*     *         *     * 
*       *     *       * 
*         * *         * 
*         * *         * 
*       *     *       * 
*     *         *     * 
*   *             *   * 
* *                 * * 
* * * * * * * * * * * * 

This Python Program prints the Hollow Square Star With Diagonals using a while loop.

# Python Program to Print Hollow Square Star With Diagonals Pattern
 
rows = int(input("Enter Hollow Square Star With Diagonals Rows = "))

print("Hollow Square Star With Diagonals Pattern") 
i = 1
while(i <= rows):
    j = 1
    while(j <= rows):
        if(i == 1 or i == rows or j == 1 or j == rows
           or i == j or j == (rows - i) + 1):
            print('* ', end = '')
        else:
            print('  ', end = '')
        j = j + 1
    print()
    i = i + 1
Python Program to Print Hollow Square Star With Diagonals 2

In this Python example, we created a hollowSquareDiagonals function to print the Hollow Square Star With Diagonals Pattern. It replaces the star in hollow Hollow Square With Diagonals with a given symbol.

# Python Program to Print Hollow Square Star With Diagonals Pattern

def hollowSquareDiagonals(rows, ch):
    for i in range(rows):
        for j in range(rows):
            if(i == 0 or i == rows - 1 or j == 0 or j == rows - 1
               or i == j or j == (rows - 1 - i)):
                print('%c ' %ch, end = '')
            else:
                print('  ', end = '')
        print()
    
rows = int(input("Enter Hollow Square Star With Diagonals Rows = "))
ch = input("Symbol to use in Hollow Square With Diagonals = " )

print("Hollow Square With Diagonals Pattern")
hollowSquareDiagonals(rows, ch)
Enter Hollow Square Star With Diagonals Rows = 15
Symbol to use in Hollow Square With Diagonals = $
Hollow Square With Diagonals Pattern
$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ 
$ $                       $ $ 
$   $                   $   $ 
$     $               $     $ 
$       $           $       $ 
$         $       $         $ 
$           $   $           $ 
$             $             $ 
$           $   $           $ 
$         $       $         $ 
$       $           $       $ 
$     $               $     $ 
$   $                   $   $ 
$ $                       $ $ 
$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ 

About Suresh

Suresh is the founder of TutorialGateway and a freelance software developer. He specialized in Designing and Developing Windows and Web applications. The experience he gained in Programming and BI integration, and reporting tools translates into this blog. You can find him on Facebook or Twitter.