Open In App

Python program to print the Inverted heart pattern

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

Let us see how to print an inverted heart pattern in Python.

Example:

Input: 11
Output:

          *
         ***
        *****
       *******
      *********
     ***********
    *************
   ***************
  *****************
 *******************
*********************
 *********  ********
  *******    ******
   *****      **** 
   
Input: 15
Output:
              *
             ***
            *****
           *******
          *********
         ***********
        *************
       ***************
      *****************
     *******************
    *********************
   ***********************
  *************************
 ***************************
*****************************
 *************  ************
  ***********    **********
   *********      ********
    *******        ******

Approach:

  1. Determine the size of the heart.
  2. Print an inverted triangle with size number of rows.
  3. Print the rest of the heart in 4 segments inside another loop.
  4. Print the white space right-triangle at the beginning.
  5. Print the first trapezium with stars.
  6. Print the white space triangle.
  7. Print the second trapezium with stars.

Python3




# determining the size of the heart
size = 15
 
# printing the inverted triangle
for a in range(0, size):
    for b in range(a, size):
        print(" ", end = "")
    for b in range(1, (a * 2)):
        print("*", end = "")
    print("")
 
# printing rest of the heart
for a in range(size, int(size / 2) - 1 , -2):
 
    # printing the white space right-triangle
    for b in range(1, size - a, 2): 
        print(" ", end = "")
 
    # printing the first trapezium
    for b in range(1, a + 1):
        print("*", end = "")
 
    # printing the white space triangle
    for b in range(1, (size - a) + 1):
        print(" ", end = "")
 
    # printing the second trapezium
    for b in range(1, a):
        print("*", end = "")
 
    # new line
    print("")


Output:

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

 Time complexity: O(S2) for given input size S

Auxiliary Space: O(1)



Previous Article
Next Article

Similar Reads

Program to Print Inverted Left Half Pyramid Pattern (Star Pattern)
Given an integer N, the task is is to print a left half pyramid pattern with N rows. In this pattern, the first row contains N stars, the second row contains N - 1 stars, and so forth until the Nth row, which contains only 1 star. All the stars are aligned to the right. Examples: Input: 3Output: *** ** * Input: 5Output: ***** **** *** ** * Approach
4 min read
Program to Print Inverted Right Half Pyramid Pattern (Star Pattern)
Given an integer N, print N rows of inverted right half pyramid pattern. In inverted right half pattern of N rows, the first row has N number of stars, second row has (N - 1) number of stars and so on till the Nth row which has only 1 star. Examples: Input: n = 5Output:*************** Input: n = 3Output:****** Approach: The problem can be solved us
3 min read
Program to Print Inverted Full Pyramid Pattern (Star Pattern)
Given an integer N, the task is to print a pattern of N rows representing an inverted full pyramid. In this pattern, the first row has (2 * N - 1) stars, the second row has (2 * N - 3) stars, and so on until the Nth row, which has only 1 star. All stars are center-aligned. Examples: Input: 3Output: ***** *** * Input: 5Output: ********* ******* ****
4 min read
Python - Print Heart Pattern
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
3 min read
Program to print V and inverted-V pattern
Inverted V pattern: Given the value of n, print the inverted V pattern.Examples : Input : n = 5 Output : E D D C C B B A A Input : n = 7 Output : G F F E E D D C C B B A A Below is the program to print the above pattern C/C++ Code // C++ Implementation to print the pattern #include <bits/stdc++.h> using namespace std; // Function definition v
8 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
Printing Heart Pattern in C
How to print below heart pattern in C? AAAAAAA AAAAAA AAAAAAAAA AAAAAAAA AAAAAAAAAAA AAAAAAAAAA AAAAAAAAAAAAA AAAAAAAAAAAA AAAAAAAAAAAAAAAAAAAAAAAAAAAAA BBBBBBBBBBBBBBBBBBBBBBBBBBBBB BBBBBBBBBBBBBBBBBBBBBBBBBBB BBBBBBBBBBBBBBBBBBBBBBBBB BBBBBBBBBBBBBBBBBBBBBBB BBBBBBBBBBBBBBBBBBBBB BBBBBBBBBBBBBBBBBBB BBBBBBBBBBBBBBBBB BBBBBBBBBBBBBBB BBBBBBBBBBBBB
8 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
Program to Print Right Half Pyramid Pattern (Star Pattern)
Given an integer N, print N rows of right half pyramid pattern. In right half pattern of N rows, the first row has 1 star, second row has 2 stars and so on till the Nth row which has N stars. All the stars are left aligned. Examples: Input: 3Output: ****** Input: 5Output: *************** Approach: The problem can be solved using two nested loops. T
3 min read
Program to Print Left Half Pyramid Pattern (Star Pattern)
Given an integer N, the task is to print N rows of left half pyramid pattern. In left half pattern of N rows, the first row has 1 star, second row has 2 stars and so on till the Nth row which has N stars. All the stars are right aligned. Examples: Input: 3Output: * ***** Input: 5Output: * ** *** ********* Approach: The problem can be solved using t
4 min read
three90RightbarBannerImg