How to print red heart in python 3
Asked Answered
C

2

5

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

Craver answered 1/10, 2020 at 18:31 Comment(4)
enjoy all python emojis including red heart and even green heart 😷 medium.com/@codingpilot25/…Odious
Why not just include the emoji itself in your code, rather than escape it?Silvasilvain
It's a composite character, so you print both, for example print(c1 + c2).Wolff
I really appreciate this question. +1 ❤️️Saw
A
4
>>> 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

Ashur answered 1/10, 2020 at 18:49 Comment(1)
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.Ashur
A
8

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.

Arie answered 1/10, 2020 at 18:45 Comment(2)
It only printed a black heart for me. What IDE are you using? I can try using that IDECraver
I tried it on Jupyter Notebook with Python 3.6. It also worked directly on my terminal by opening a python3 environment.Arie
A
4
>>> 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

Ashur answered 1/10, 2020 at 18:49 Comment(1)
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.Ashur

© 2022 - 2024 — McMap. All rights reserved.