Using Emoji in Python
Programming is much more than just printing “Hello World!”
Programming is fun and much more than just printing “ Hello World!” and number series. We can print emojis and get our hands dirty as much as we want. Thanks to the preferred installer program or pip, the one destination solution to all python problems! We can print emojis in three ways:
- Unicodes
- CLDR (Common Locale Data Repository) names
- emoji module
Unicodes
You can get an entire list of Emojis Unicodes from here.
Every emoji has a Unicode associated with it. It starts with “U” followed by three zeroes (which is abbreviated by a +) and is 9 characters long. eg. “U0001F603”, “U0001F600” etc. In a nutshell, when you see something like this U+1F916, just replace “+” with “000”. While giving these Unicode as input, we need to prefix it by “\”.
input:
print(“\U0001F603”); #grinning face with big eyes
print(“\U0001F600”); #grinning face
print("\U0001F916"); #robot
output:
Using CLDR
As every emoji is associated with a Unicode, in a similar manner, every emoji comes with a short name which can also be used to print them.
input:
print(“\N{grinning face}”) # grinning face
print(“\N{thinking face}”) # slightly smiling faceprint(“\N{winking face}”) # winking face
output:
Using emoji module
Check this cheat sheet for an entire list of the emoji module names.
An alternative of the above two methods is using the emoji modules in order to print emojis. This requires the installation of emoji pip package:
pip install emoji
The emojize()
function requires the CLDR short name to be passed in it as the parameter. It then returns the corresponding emoji. One important thing to keep in mind is to replace every space with an underscore in the CLDR name string
input:
print(emoji.emojize(“:zipper-mouth_face:”))
print(emoji.emojize(“:musical_note:”))
print(emoji.emojize(“:fire:”))
output: