0
0
PythonProgramBeginner · 2 min read

Python Program to Print Heart Pattern

You can print a heart pattern in Python using nested for loops and if conditions to print stars in the shape of a heart, for example: for row in range(6): for col in range(7): print('*' if (row == 0 and col % 3 != 0) or (row == 1 and col % 3 == 0) or (row - col == 2) or (row + col == 8) else ' ', end=''); print().
📋

Examples

InputNo input needed
Output ** ** * * * * * * * * * * * **
InputNo input needed
Output ** ** * * * * * * * * * * * **
InputNo input needed
Output ** ** * * * * * * * * * * * **
🧠

How to Think About It

To print a heart pattern, think of the shape as a grid of rows and columns. Use loops to go through each position and decide if a star or space should be printed based on the position's relation to the heart shape. The heart can be formed by combining two upper arcs and a lower triangle shape.
📐

Algorithm

1
Loop through each row of the pattern
2
Inside each row, loop through each column
3
Check if the current position matches the heart shape conditions
4
If yes, print a star; otherwise, print a space
5
Move to the next line after each row
💻

Code

python
for row in range(6):
    for col in range(7):
        if (row == 0 and col % 3 != 0) or 
           (row == 1 and col % 3 == 0) or 
           (row - col == 2) or 
           (row + col == 8):
            print('*', end='')
        else:
            print(' ', end='')
    print()
Output
** ** * * * * * * * * * * *
🔍

Dry Run

Let's trace the heart pattern printing for the first two rows.

1

Row 0, Columns 0 to 6

Positions 1, 2, 4, 5 print '*', others print ' '

2

Row 1, Columns 0 to 6

Positions 0, 3, 6 print '*', others print ' '

RowColCondition MetPrint
00False' '
01True'*'
02True'*'
03False' '
04True'*'
05True'*'
06False' '
10True'*'
11False' '
12False' '
13True'*'
14False' '
15False' '
16True'*'
💡

Why This Works

Step 1: Upper arcs of the heart

The first two rows create the two rounded tops of the heart using conditions on row and column positions.

Step 2: Lower triangle shape

The conditions row - col == 2 and row + col == 8 create the slanting sides of the heart's bottom.

Step 3: Printing stars and spaces

Stars are printed where conditions are true to form the heart shape; spaces fill the rest to keep the shape clear.

🔄

Alternative Approaches

Using string patterns
python
heart = [
  '  ** **  ',
  ' *  *  * ',
  '*      *',
  '*      *',
  ' *    * ',
  '  *  *  ',
  '   **   '
]
for line in heart:
    print(line)
This method is simpler and uses predefined strings but is less flexible for resizing.
Using mathematical equation
python
for y in range(15, -15, -1):
    for x in range(-30, 30):
        if (x*0.04)**2 + (y*0.1)**2 - 1 <= 0 and (x*0.04)**2 + (y*0.1)**2 - 1 >= -0.3:
            print('*', end='')
        else:
            print(' ', end='')
    print()
This uses math to draw a smooth heart shape but is more complex and slower.

Complexity: O(n*m) time, O(1) space

Time Complexity

The program uses nested loops over rows (n) and columns (m), so time grows with the size of the pattern.

Space Complexity

Only a few variables are used; printing is done directly, so space is constant.

Which Approach is Fastest?

Using predefined strings is fastest but less flexible; loop-based drawing is more flexible but slightly slower.

ApproachTimeSpaceBest For
Loop with conditionsO(n*m)O(1)Flexible patterns
Predefined stringsO(n)O(n*m)Simple fixed patterns
Mathematical equationO(n*m)O(1)Smooth shapes, complex
💡
Use nested loops and carefully chosen conditions to shape the heart with stars.
⚠️
Beginners often forget to print a newline after each row, causing the pattern to print on one line.