This article explains how to convert emojis to text and text to emojis using custom code and using the third-party Python Emot and Emoji libraries.

Emojis convey useful information about the sentiment of a text. Natural language processing (NLP) applications can exploit emojis to detect the meaning of the text. NLP text generation models, like GPT-2, GPT-J and GPT-Neo can even generate text containing emojes. For many NLP algorithms to leverage emojis, you first need to find textual representations of emojis. Therefore, converting emojis to text is an important NLP task.

On the flip side converting text to emojis is also important. Particularly when you want to store text strings containing emojis in a database. In a scenario like this, you’ll need to convert text back to emojis before it can be rendered to users.

Before we explain how to convert emojis to text and text back to emojis using third party Python libraries, let’s step through a code showing how you can manually perform this task without the help of any external libraries.

Manually Converting Emojis to Text

There are multiple ways to convert emojis to text. One of the simplest methods would be to maintain a dictionary that contains emojis as keys and their text representations as values. The following script defines an emoji dictionary with three emojis. You can add more emojis if you want.

emoji_dictionary = {'👍': '[thumbs_up]',
                   '❤️':'[red_heart]',
                    '😞':'[sad_face]'}

emoji_dictionary['❤️']

Output:

'[red_heart]'

To convert emojis in the input text string to text, you can split the input text and then perform token matching of each word in the input with the emoji dictionary keys. If a token is found in the dictionary keys, you can replace the emoji token with the corresponding text. The following script converts the emojis in the input text to textual form.

text = 'I love Python ❤️ , it is brilliant 👍'
text_tokens = text.split(" ")
print(text_tokens)

new_text = ""
for i in text_tokens:
    if i in emoji_dictionary:
        new_text +=  " " +emoji_dictionary[i]
    else:
        new_text += " " + i

print(new_text)

Output:

['I', 'love', 'Python', '❤️', ',', 'it', 'is', 'brilliant', '👍']
 I love Python [red_heart] , it is brilliant [thumbs_up]

Manually Converting Text to Emojis

Converting text back to emojis is very similar to converting emojis to text. One way to do this is by swapping the keys and values in your emoji dictionary such that keys now represent text and the emojis represent values. Here’s how you can do that:

emoji_dictionary = {'👍': '[thumbs_up]',
                   '❤️':'[red_heart]',
                    '😞':'[sad_face]'}

emoji_dictionary2 = dict([(value, key) for key, value in emoji_dictionary.items()])

print(emoji_dictionary2)

Output:

{'[thumbs_up]': '👍', '[red_heart]': '❤️', '[sad_face]': '😞'}

Next, you can split the input string and perform token matching with the emojis dictionary, as shown in the script below. In the output, you can see emojis in the text.

text = 'I love Python [red_heart] , it is brilliant [thumbs_up]'
text_tokens = text.split(" ")

new_text = ""
for i in text_tokens:
    if i in emoji_dictionary2:
        new_text += " " + emoji_dictionary2[i]
    else:
        new_text += " " + i

print(new_text)

Output:

I love Python ❤️ , it is brilliant 👍

Manually converting text to emojis and emojis back to text can be cumbersome due to the quantity of emojis and the iterations involved, but it’s certainly possible. Third party Python libraries make this task easier.


Code More, Distract Less: Support Our Ad-Free Site

You might have noticed we removed ads from our site - we hope this enhances your learning experience. To help sustain this, please take a look at our Python Developer Kit and our comprehensive cheat sheets. Each purchase directly supports this site, ensuring we can continue to offer you quality, distraction-free tutorials.


Using the Python Emot Library

The Python Emot library functions can detect emojis and emoticons in text.

You can install the Emot library with the following pip command:

!pip install emot

Next, you need to create an object of the emot.core.emot class.

import emot

emot_object = emot.core.emot()

Detecting Emojis

The emoji() function from the emot.core.emot class finds emojis from the input text. The method output contains the detected emojis, along with their location and meaning. Here’s an example:

text = "This is a brilliant movie 👌"
emot_object.emoji(text)

Output:

{'value': ['👌'], 'location': [[26, 27]], 'mean': [':OK_hand:'], 'flag': True}

Similarly, the following script detects two emojis in the input text.

text = "I love Python ❤️, it is brilliant 👍"
emot_object.emoji(text)

Output:

{'value': ['❤', '👍'],
 'location': [[14, 15], [34, 35]],
 'mean': [':red_heart:', ':thumbs_up:'],
 'flag': True}

Detecting Emoticons

In addition to emojis, you can detect emoticons in the input text using the emoticons() function. Here’s an example. The output shows the detected emoticons, their locations in the text, and a list of their possible meanings.

text = "My phone's battery died :/, do you have a charger? :("
emot_object.emoticons(text)

Output:

{'value': [':/', ':('],
 'location': [[24, 26], [51, 53]],
 'mean': ['Skeptical, annoyed, undecided, uneasy or hesitant',
  'Frown, sad, andry or pouting'],
 'flag': True}

Notice the typo of “angry” written as “andry” in version 3.1 of the emot library. I created a pull request to correct this typo and I suspect it will be corrected soon.

Using the Python Emoji Library

If the emot library doesn’t meet your needs, you could use the Python Emoji library instead to help you convert emojis to text and vice versa. The following script installs the library.

!pip install emoji

Converting Emojis to Text

The demojize() method from the emoji module lets you convert emojis to text. Here’s an example.

import emoji

text = 'I love Python ❤️, it is brilliant 👍'
emoji.demojize(text )

Output:

'I love Python :red_heart:, it is brilliant :thumbs_up:'

The language attribute of the demojize() method lets you set the language for converting emojis to text. For instance, the following script converts emojis to their Spanish counterpart in text.

text = 'I love Python ❤️, it is brilliant 👍'
emoji.demojize(text,  language='es')

Output:

'I love Python :corazón_rojo:, it is brilliant :pulgar_hacia_arriba:'

You can further checkout the official documentation to see the list of all emojis, languages, and their corresponding textual representations.

Converting Text to Emojis

Finally, you can convert the text back to emojis using the emojize() function. Here’s an example on how to do this:

text = 'I love Python :red_heart:, it is brilliant :thumbs_up: '
emoji.emojize(text)

Output:

'I love Python ❤️, it is brilliant 👍 '

If you found this tutorial helpful, consider subscribing using the form below and we’ll send you a few more of our favorite Python tutorials!


Code More, Distract Less: Support Our Ad-Free Site

You might have noticed we removed ads from our site - we hope this enhances your learning experience. To help sustain this, please take a look at our Python Developer Kit and our comprehensive cheat sheets. Each purchase directly supports this site, ensuring we can continue to offer you quality, distraction-free tutorials.