用于打印表情符号的Python程序
我们可以通过多种方式在Python中打印 Emoji。让我们看看如何使用 Unicode、CLDR 名称和表情符号模块打印表情符号。
使用 Unicode:
每个表情符号都有一个与之关联的 Unicode。表情符号还有一个 CLDR 简称,也可以使用。
从 unicode 列表中,将“+”替换为“000”。例如 – “U+1F600” 将变为 “U0001F600” 并在 unicode 前面加上“\” 并打印出来。
Python3
# grinning face
print("\U0001f600")
# grinning squinting face
print("\U0001F606")
# rolling on the floor laughing
print("\U0001F923")
Python3
# grinning face
print("\N{grinning face}")
# slightly smiling face
print("\N{slightly smiling face}")
# winking face
print("\N{winking face}")
Python3
# import emoji module
import emoji
print(emoji.emojize(":grinning_face_with_big_eyes:"))
print(emoji.emojize(":winking_face_with_tongue:"))
print(emoji.emojize(":zipper-mouth_face:"))
输出:
使用 CLDR 短名称:
Python3
# grinning face
print("\N{grinning face}")
# slightly smiling face
print("\N{slightly smiling face}")
# winking face
print("\N{winking face}")
输出:
使用表情符号模块:
表情符号也可以使用Python中提供的表情符号模块来实现。要安装它,请在终端中运行以下命令。
pip install emoji
emojize()函数需要将 CLDR 短名称作为参数传入其中。然后它返回相应的表情符号。将 CLDR 短名称中的空格替换为下划线。
Python3
# import emoji module
import emoji
print(emoji.emojize(":grinning_face_with_big_eyes:"))
print(emoji.emojize(":winking_face_with_tongue:"))
print(emoji.emojize(":zipper-mouth_face:"))
输出:
demojize()函数将传递的表情符号转换为其相应的 CLDR 短名称。
以下是一些常见的表情符号 Unicode 及其 CLDR 短名称的列表:
CLDR Short Name Unicode grinning face
U+1F600 grinning face with big eyes
U+1F603 grinning face with smiling eyes
U+1F604 beaming face with smiling eyes
U+1F601 grinning squinting face
U+1F606 grinning face with sweat
U+1F605 rolling on the floor laughing
U+1F923 face with tears of joy
U+1F602 slightly smiling face
U+1F642 upside-down face
U+1F643 winking face U+1F609 smiling face with smiling eyes
U+1F60A smiling face with halo
U+1F607 smiling face with 3 hearts U+1F970 smiling face with heart-eyes U+1F60D star-struck
U+1F929 face blowing a kiss
U+1F618 kissing face
U+1F617 smiling face
U+263A kissing face with closed eyes
U+1F61A kissing face with smiling eyes
U+1F619 face savoring food
U+1F60B face with tongue
U+1F61B winking face with tongue
U+1F61C zany face
U+1F92A squinting face with tongue
U+1F61D money-mouth face
U+1F911 hugging face
U+1F917 face with hand over mouth
U+1F92D shushing face
U+1F92B thinking face
U+1F914 zipper-mouth face
U+1F910 face with raised eyebrow
U+1F928 neutral face
U+1F610 expressionless face
U+1F611 face without mouth
U+1F636 smirking face
U+1F60F unamused face
U+1F612 face with rolling eyes
U+1F644 grimacing face
U+1F62C lying face
U+1F925 relieved face
U+1F60C pensive face
U+1F614 sleepy face
U+1F62A drooling face
U+1F924 sleeping face
U+1F634 face with medical mask
U+1F637 face with thermometer
U+1F912 face with head-bandage
U+1F915 nauseated face
U+1F922