4

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")

David W
  • 158
  • 1
  • 2
  • 9

2 Answers2

6

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.

Akanksha Atrey
  • 780
  • 4
  • 8
3
>>> 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

snakecharmerb
  • 47,570
  • 11
  • 100
  • 153
tripleee
  • 175,061
  • 34
  • 275
  • 318
  • For what it's worth, in the mobile version of this site, I see a red heart in the code block, too (Firefox on iPhone). But then it renders the undecorated U+2764 glyph as a red heart, too. – tripleee Oct 01 '20 at 19:24