How to add Emojis in a Python Program

Hello everyone, In this tutorial, we’ll be learning how we can add Emojis in a Python Program. Emojis are a visual representation of an emotion or an entity from various genres. It can be used to make something more readable and is a very good way to show emotions and feelings. We will make use of External Python library named ‘Emoji’ to add emojis and to decode an emoji into its text representation. Let us start this tutorial by Installing the required library.

Installing Emoji

To install Emoji, Goto Command line and run the following command.

pip install emoji

Now we can make use of emoji in our Python Program.

Using Emoji with Python

The first step is to import the required library.

import emoji

Now let us Print a sentence with emoji. For this, we will be using emojize() which can make an emoji out of a specific text and see what we will get.

print(emoji.emojize('Python is a dangerous :snake:)

The output will look something like this.

Python is a dangerous 🐍

Looking nice. Now let us decode an emoji to its textual representation. Taking the previous output we will give it as an argument in a function named demojize() and see the result.

print(emoji.demojize('Python is a dangerous 🐍'))

The output of the above code will be.

Python is a dangerous :snake:

But whenever we want to write a sentence with emoji we have to give ‘:’ as prefix and suffix to the text-representation of an emoji like :snake: and for every sentence, we are required to use emojize(). To solve these we can create a function like the one below.

def make_emoji(word):
    res = emoji.emojize(f"\t:{word}:\t", delimiters = ("\t","\t"))
    return res

print('He is a cute',make_emoji('baby'))
He is a cute 👶

We have created a function that takes the word and will convert it into the corresponding emoji. We have shown the use of delimiters that takes two strings which represent the prefix and suffix with the word. Now it becomes easy for us to use emoji in our Program.

The complete list of emoji with their text-representation can be found here.

We hope you like this tutorial and if you have any doubts, feel free to leave a comment below.

You may like to learn.

Load and show image using Matplotlib Python library

How to Calculate the Execution Time of a Small Python Program

Calculating With Dictionaries in Python

Leave a Reply

Your email address will not be published. Required fields are marked *