David W
David W

Reputation: 168

How to print red heart in python 3

I need to print the red heart emoji ❤️️ with unicode in Python 3 but it has two unicodes (\U00002764 and \U0000FE0F). How am I suppose to print it?

For example, a green heart is print("\U0001F49A")

Upvotes: 5

Views: 25579

Answers (2)

tripleee
tripleee

Reputation: 189679

>>> print("\u2764\ufe0f")
❤️

Whether it "works" depends on the font you have and which glyphs it supports. Here's the same character in a non-code font (literally copy/pasted from the above):

❤️

In some more detail, U+2764 is HEAVY BLACK HEART and typically renders as a black heart: ❤

U+FE0F is VARIATION SELECTOR 16 which is a combining character which modifies the previous character, in this case to turn the heart red.

Neither of these code points has more than four hex digits so using the long-hand \U00000000 form is unnecessary.

(For what it's worth, in the text above, I see a black heart in the code font, and a red one in the regular body text font. In the MacOS Terminal, the red heart renders in a different font, so there I see it as a red heart in the Python REPL, too.)

Screen shot showing how the character renders, MacOS Catalina

Upvotes: 4

Akanksha Atrey
Akanksha Atrey

Reputation: 880

Neither of the two unicodes worked for me - they both printed black hearts. The red heart requires combining the two unicodes:

print("\u2764\uFE0F")

Explanation for why it requires two unicodes can be found here.

Upvotes: 8

Related Questions