Python Program to Print a Hollow Diamond Star Pattern

In this article, we will write a Python program to print a hollow diamond star pattern. The program takes the number of rows that the diamond should have from the user as input and prints a hollow diamond on the output screen.

Before diving into the code, let’s understand the structure of the hollow diamond star pattern. The pattern is created in two parts: the upper hollow pyramid and the lower hollow pyramid. When these two parts are combined, they form the complete diamond.

Let’s have a look at the code part first and then we will break it down to understand how does it works:

# Get the Number of Rows
row = int(input('Enter the Number of Rows: '))

# Print the Upper part of the hollow diamond
for i in range(1, row + 1):
    
    # Print spaces in the upper part of diamond
    for j in range(1, row - i + 1):
        print(" ", end="")
        
    # Print stars on the left and right of upper half    
    for j in range(1, 2 * i):
        if j == 1 or j == 2 * i - 1:
            print("*", end="")
        else:
            print(" ", end="")
    print()  # New line

# Print the Lower part of hollow diamond
for i in range(row - 1, 0, -1):
    
    # Print spaces in the lower part of diamond
    for j in range(1, row - i + 1):
        print(" ", end="")
    
    # Print stars on the left and right of lower half    
    for j in range(1, 2 * i):
        if j == 1 or j == 2 * i - 1:
            print("*", end="")
        else:
            print(" ", end="")
    print()  # New line

Output:

Enter the Number of Rows: 5
        *
      *  *
    *      *
  *          *
*              *
  *          *
    *      *
      *  *
        *

Program Explanation:

The program starts by taking the number of rows from the user as input. We have used the input() function for that.

To print the full pyramid, we have break it down into two parts, the upper one and the lower one.

  1. Upper Part of Hollow Diamond:
    • for i in range(1, row + 1):: Loop for each row in the upper part of the diamond.
    • for j in range(1, row - i + 1):: Prints leading spaces for each row in the upper part.
    • for j in range(1, 2 * i):: Prints asterisks for each row, with conditions to create a hollow pattern.
    • print(): Moves to the next line after completing a row in the upper part.
  2. Lower Part of Hollow Diamond:
    • for i in range(row - 1, 0, -1):: Loop for each row in the lower part of the diamond, in reverse order.
    • for j in range(1, row - i + 1):: Prints leading spaces for each row in the lower part.
    • for j in range(1, 2 * i):: Prints asterisks for each row, with conditions to create a hollow pattern.
    • print(): Moves to the next line after completing a row in the lower part.

When the entire code runs, we get the full hollow diamond pattern as a result.

That’s all for this article. Thanks for reading!

Author

  • Manoj Kumar

    Hi, My name is Manoj Kumar. I am a full-stack developer with a passion for creating robust and efficient web applications. I have hands-on experience with a diverse set of technologies, including but not limited to HTML, CSS, JavaScript, TypeScript, Angular, Node.js, Express, React, and MongoDB.

    View all posts

Leave a Comment