Editor

Python Program to Print a Heart Pattern

Intermediate · Patterns

A heart pattern draws a heart out of stars: two rounded humps on top and a point at the bottom. This program builds it from shapes you already know, two small pyramids side by side for the humps and an upside-down pyramid for the point.

Program

heart_pattern.py
size = 4 # try 3 to 6; or: size = int(input("Enter the size: "))
# Top: two humps side by side, each a small pyramid of 3, 5, 7 stars
for i in range(size - 1):
hump = "*" * (2 * i + 3)
edge = " " * (size - 2 - i) # spaces before the left hump
gap = " " * (2 * size - len(hump)) # spaces between the humps
print(edge + hump + gap + hump)
# Bottom: an upside-down pyramid from 13 stars down to 1
for i in range(2 * size - 1):
print(" " * (i + 1) + "*" * (4 * size - 3 - 2 * i))
Output
  ***     ***
 *****   *****
******* *******
 *************
  ***********
   *********
    *******
     *****
      ***
       *

How it works

  1. The top of the heart is two humps, and each hump is a small pyramid whose row i has 2 * i + 3 stars: 3, then 5, then 7.
  2. edge indents the left hump one space less on each row, and gap, the space between the humps, shrinks as they grow until a single space separates them.
  3. The bottom is an inverted pyramid whose first row has 4 * size - 3 stars (13 when size is 4), just narrower than the top, so the sides curve inwards.
  4. Each bottom row is indented one more space and loses 2 stars, ending in a single star at the point.
  5. Every part is worked out from size, so changing that one number draws a smaller or bigger heart.

Other ways to write it

Hollow heart from a grid

Hollow heart from a grid
for row in range(6):
for col in range(7):
humps = (row == 0 and col % 3 != 0) or (row == 1 and col % 3 == 0)
sides = row - col == 2 or row + col == 8
if humps or sides:
print("*", end=" ")
else:
print(" ", end=" ")
print()
Output
  * *   * *   
*     *     * 
*           * 
  *       *   
    *   *     
      *

The heart is drawn on a 6 by 7 grid with a rule for each part: the two humps on the first two rows, and two diagonal lines, row - col == 2 and row + col == 8, that meet at the point.

Using the heart curve equation

Using the heart curve equation
# Points where (x*x + y*y - 1)**3 - x*x * y**3 <= 0 lie inside a heart
for row in range(9, -9, -1):
y = row / 8
line = ""
for col in range(-19, 20):
x = col / 16 # x moves in smaller steps: characters are tall and thin
if (x * x + y * y - 1) ** 3 - x * x * y ** 3 <= 0:
line += "*"
else:
line += " "
print(line)
Output
      ************   ************      
   *********************************   
  ***********************************  
  ***********************************  
 ************************************* 
 ************************************* 
  ***********************************  
  ***********************************  
   *********************************   
   *********************************   
     *****************************     
      ***************************      
        ***********************        
          *******************          
            ***************            
               *********               
                 *****                 
                   *

Mathematicians have a formula whose solutions form a heart, so the program checks every point of a grid and prints a star wherever the formula is true, which gives the heart smooth curves.

Try it yourself

Change size to 6 for a bigger heart, then replace the stars with @.

Edit the program above and press Run, or open it in the full editor to save it and share it. To watch it run line by line, open it in the visualizer.

All Python programs · CBSE class 12 practical file · Python exercises