Python Program to Print Heart Shape Pattern

Introduction

Printing a heart shape using stars (*) is a fun and interesting exercise to understand how to use loops and conditional logic to position stars and spaces. The heart shape requires careful positioning of stars to create the upper and lower parts.

Problem Statement

Create a Python program that:

  • Prints a heart-shaped pattern using stars (*).
  • Uses nested loops and conditional logic to control the placement of stars and spaces.

Example:

  • Output:
     ***   ***
    ***** *****
    ***********
     *********
      *******
       *****
        ***
         *
    

Solution Steps

  1. Divide the Heart into Two Parts: The heart consists of an upper part with two semi-circles and a lower part that resembles a downward-pointing triangle.
  2. Use Nested Loops: The outer loops will control the rows, and the inner loops will handle printing stars and spaces.
  3. Display the Heart Shape: Use conditional logic to print stars and spaces in the correct positions.

Python Program

# Step 1: Define the size of the heart pattern
size = 6

# Step 2: Print the upper part of the heart (two semi-circles)
for i in range(size // 2, size + 1, 2):
    # Print leading spaces
    print(" " * ((size - i) // 2), end="")
    
    # Print stars for the first semi-circle
    print("*" * i, end="")
    
    # Print spaces between the two semi-circles
    print(" " * (size - i), end="")
    
    # Print stars for the second semi-circle
    print("*" * i)

# Step 3: Print the lower part of the heart (inverted triangle)
for i in range(size, 0, -1):
    # Print leading spaces
    print(" " * (size - i), end="")
    
    # Print stars to form the downward triangle
    print("*" * (2 * i - 1))

Explanation

Step 1: Define the Size of the Heart Pattern

  • The size controls the height of the heart shape. It is set to 6 for this example.

Step 2: Print the Upper Part of the Heart

  • The outer loop controls the rows for the upper part of the heart.
  • The inner loops handle:
    • Printing spaces to align the stars for the first semi-circle.
    • Printing stars for the first semi-circle.
    • Printing spaces between the two semi-circles.
    • Printing stars for the second semi-circle.

Step 3: Print the Lower Part of the Heart

  • The second loop handles printing the inverted triangle, which forms the lower part of the heart.
    • Leading spaces are printed for alignment.
    • Stars are printed in decreasing order to form the inverted triangle.

Output Example

For size = 6, the output will be:

  ***   ***
 ***** *****
***********
 *********
  *******
   *****
    ***
     *

For size = 8, the output will be:

   ***     ***
  *****   *****
 ******* *******
***************
 *************
  ***********
   *********
    *******
     *****
      ***
       *

Conclusion

This Python program prints a heart shape pattern using stars (*). It uses nested loops and conditional logic to control the placement of stars and spaces, creating a heart shape. This exercise is a great way to practice using loops and formatting output in Python.

Comments