Emojis in Python - The Emoji Module

Emojis have, in many places, replaced texts. Now, every chat, whether personal or public, is filled with emojis. With emojis everywhere, why should Python miss out? That's what the creators of the Emoji module must have thought when they came up with this fantastic module. 

The emoji module offers you a feature of emojizing textual codes into graphical emojis. It also gives you a function to find the code for a specific emoji.

As the module is not inbuilt, we do need to install it. You can use this pip function to install the module:

pip install emoji

Now that the module is usable, let's get into the main functions:

The first and probably most important function is the emojize() function. The emojize function takes a textual code and converts it to a graphical emoji. Here's an example...

import emoji
print(emoji.emojize("I am a Pygrammer :thumbs_up:"))

The textual code is usually a word or string of words separated by the _ (underscore) symbol and placed between two : (colon) symbols. If the code is valid, the output will print the emojized version of the code, like this...

I am a Pygrammer 👍

It works. 

Now, what if you have an emoji and you need the code for that specific emoji? The Emoji module offers a function for that too. The demojize() function gives you the code for the emoji entered, like this...

import emoji
print(emoji.demojize('I am happy 😀'))

The output for this would be like this...

I am happy :grinning_face:

meaning that the textual code for 😀 is :grinning_face: 

The Emoji module also offers a few other functions, such as the is_emoji() function. This function returns a boolean value depending on whether the character in between the parenthesis is an emoji or not. For example:

import emoji
print(emoji.is_emoji('👌'))

The output:

True
>>> 

Another cool thing about the Emoji module is that it supports 5 languages apart from English. People who prefer Spanish, Portuguese, Italian, French, or German can use the module in the language of their choice. Here's how you do that:

import emoji
print(emoji.emojize('Python es :cara_sonriente:', language='es'))

and the output:

Python es ☺️
>>> 

A similar syntax can be used for the other languages...

Here's a list of all the emojis with their codes for easy usage: English Emoji List

If you found this post interesting, do share and follow The Pygrammer. The comment section is open for all queries or suggestions, so feel free to use it. 

Until next time... 

Comments