Open In App

Python – Print Heart Pattern

Last Updated : 05 Aug, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

Given an even integer input, the task is to write a Python program to print a heart using loops and mathematical formulations.

Example :

For n = 8

  *   *   *   *   
*       *       * 
*               * 
*     G F G     * 
  *           *   
    *       *     
      *   *       
        *

For n = 14

      * *           * *       
    *     *       *     *     
  *         *   *         *   
*             *             * 
*                           * 
*           G F G           * 
  *                       *   
    *                   *     
      *               *       
        *           *         
          *       *           
            *   *             
              *       

Approach :

The following steps are used :

  • Form the worksheet of n X n+1 using two loops.
  • Apply the if-else conditions for printing stars.
  • Apply the if-else conditions for printing text “GFG”.
  • Apply else condition for rest spaces.

Note: The value of n must be greater than 8

Below is the implementation of the above approach :

Python3




# define size n = even only
n = 8
 
# so this heart can be made n//2 part left,
# n//2 part right, and one middle line
# i.e; columns m = n + 1
m = n+1
 
# loops for upper part
for i in range(n//2-1):
    for j in range(m):
         
        # condition for printing stars to GFG upper line
        if i == n//2-2 and (j == 0 or j == m-1):
            print("*", end=" ")
             
        # condition for printing stars to left upper
        elif j <= m//2 and ((i+j == n//2-3 and j <= m//4) \
                            or (j-i == m//2-n//2+3 and j > m//4)):
            print("*", end=" ")
             
        # condition for printing stars to right upper
        elif j > m//2 and ((i+j == n//2-3+m//2 and j < 3*m//4) \
                           or (j-i == m//2-n//2+3+m//2 and j >= 3*m//4)):
            print("*", end=" ")
             
        # condition for printing spaces
        else:
            print(" ", end=" ")
    print()
 
# loops for lower part
for i in range(n//2-1, n):
    for j in range(m):
         
        # condition for printing stars
        if (i-j == n//2-1) or (i+j == n-1+m//2):
            print('*', end=" ")
             
        # condition for printing GFG
        elif i == n//2-1:
             
            if j == m//2-1 or j == m//2+1:
                print('G', end=" ")
            elif j == m//2:
                print('F', end=" ")
            else:
                print(' ', end=" ")
                 
        # condition for printing spaces
        else:
            print(' ', end=" ")
             
    print()


Output:

                              
      * *           * *       
    *     *       *     *     
  *         *   *         *   
*             *             * 
*                           * 
*           G F G           * 
  *                       *   
    *                   *     
      *               *       
        *           *         
          *       *           
            *   *             
              *               

Time complexity: O(n2) for given input n

Auxiliary Space: O(1)



Previous Article
Next Article

Similar Reads

Python program to print the Inverted heart pattern
Let us see how to print an inverted heart pattern in Python. Example: Input: 11 Output: * *** ***** ******* ********* *********** ************* *************** ***************** ******************* ********************* ********* ******** ******* ****** ***** **** Input: 15 Output: * *** ***** ******* ********* *********** ************* ***********
2 min read
Python program display any message on heart
This article focuses on discussing how to draw a heart with a beautiful message to your loved ones to express what you feel about them. Three types of variants of the above problem statement will be discussed here: Red Heart with a message.Pink-Red Heart with a message.Pink Heart with a message in a different font and color than in the above two ca
3 min read
Draw Heart Using Turtle Graphics in Python
Turtle is an inbuilt module in Python. It provides: Drawing using a screen (cardboard).Turtle (pen). To draw something on the screen, we need to move the turtle (pen) and to move the turtle, there are some functions like the forward(), backward(), etc Prerequisite: Turtle Programming Basics Draw Heart Using Turtle Graphics In this section, we will
2 min read
Python Program to print digit pattern
The program must accept an integer N as the input. The program must print the desired pattern as shown in the example input/ output. Examples: Input : 41325 Output : |**** |* |*** |** |***** Explanation: for a given integer print the number of *'s that are equivalent to each digit in the integer. Here the first digit is 4 so print four *sin the fir
3 min read
Python program to print checkerboard pattern of nxn using numpy
Given n, print the checkerboard pattern for a n x n matrix Checkerboard Pattern for n = 8: It consists of n * n squares of alternating 0 for white and 1 for black. We can do the same using nested for loops and some if conditions, but using Python's numpy library, we can import a 2-D matrix and get the checkerboard pattern using slicing. W2'll be us
3 min read
Python | Print an Inverted Star Pattern
An inverted star pattern involves printing a series of lines, each consisting of stars (*) that are arranged in a decreasing order. Here we are going to print inverted star patterns of desired sizes in Python Examples: 1) Below is the inverted star pattern of size n=5 (Because there are 5 horizontal lines or rows consist of stars). ***** **** *** *
2 min read
Python 3 | Program to print double sided stair-case pattern
Below mentioned is the Python 3 program to print the double-sided staircase pattern. Examples: Input: 10Output: * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * Note: This code only works for even values of n. Example1 The given Python code demonstrates a staircase pattern. It d
3 min read
Program to print a doormat pattern having a string written in the center in Python
Given the number of rows R and the number of columns C, the task is to print a doormat pattern as shown in the examples below. The number of columns in the mat's design must be 3 times the number of rows in the design. The doormat should have GeeksforGeeks printed in the center. The width of the mat is equal to the number of columns in the pattern.
2 min read
Python Program to print hollow half diamond hash pattern
Give an integer N and the task is to print hollow half diamond pattern. Examples: Input : 6 Output : # # # # # # # # # # # # # # # # # # # # Input : 7 Output : # # # # # # # # # # # # # # # # # # # # # # # # Approach: The idea is to break the pattern into two parts: Upper part: For the upper half start the for loop with iterator (i) from 1 to n and
4 min read
Python Program to Print Alphabetic Triangle Pattern
In this article, we examine a recursive method for printing character patterns in Python. Nesting loops are used in the conventional iterative approach to regulating character spacing and printing. We have provided the answer using both methods. This method prints an alphabet pattern in the form of a pyramid. It uses nested loops to print the space
4 min read
Practice Tags :