Skip to content

How to Convert Emojis to Text in Python

Learn how to convert emojis to text in Python using the emoji and demoji libraries for NLP preprocessing, sentiment analysis, and machine learning pipelines.

Emojis carry significant semantic meaning in text data. For natural language processing, sentiment analysis, or machine learning pipelines, converting emojis to their textual descriptions preserves this information that would otherwise be lost during text cleaning.

This guide covers conversion techniques using popular Python libraries.

The emoji library provides bidirectional conversion between Unicode emojis and text shortcodes:

pip install emoji
import emoji

text = "Python is 🔥 and 🐍 is awesome! 💯"

converted = emoji.demojize(text)
print(converted)

restored = emoji.emojize(converted)
print(restored)

Output:

Python is :fire: and :snake: is awesome! :hundred_points:
Python is 🔥 and 🐍 is awesome! 💯

Customizing Delimiters

import emoji

text = "Great job! 👍✨"

result = emoji.demojize(text, delimiters=(" [", "] "))
print(result)

result = emoji.demojize(text, delimiters=(" ", " "))
print(result)

Output:

Great job!  [thumbs_up]  [sparkles]
Great job!  thumbs_up  sparkles

Language Support

import emoji

text = "Hello 👋 World 🌍"

result = emoji.demojize(text, language='es')
print(result)

Output:

Hello :mano_saludando: World :globo_terráqueo_mostrando_europa_y_áfrica:

Using demoji for Descriptive Text

When you need official Unicode descriptions rather than shortcodes, use demoji:

pip install demoji
import demoji

demoji.download_codes()

text = "I love this! ❤️🚀 So exciting! 🎉"

clean_text = demoji.replace_with_desc(text, sep=" ")
print(clean_text)

emoji_dict = demoji.findall(text)
print(emoji_dict)

Removing Emojis Entirely

Sometimes you need to strip emojis without replacement:

import emoji

def remove_emojis(text):
    """Remove all emojis from text."""
    return emoji.replace_emoji(text, replace='')

text = "Hello 👋 World 🌍!"
clean = remove_emojis(text)
print(clean)  # Output: Hello  World !

clean = ' '.join(clean.split())
print(clean)  # Output: Hello World!

Using demoji for Removal

import demoji

text = "Check this out! 🔥💯🚀"

clean = demoji.replace(text, "")
print(clean)  # Output: "Check this out!"

Extracting Emojis for Analysis

import emoji

def extract_emojis(text):
    """Extract all emojis from text."""
    return [char for char in text if char in emoji.EMOJI_DATA]

text = "Having a great day! 😊🌞 Let's celebrate 🎉"
emojis = extract_emojis(text)
print(emojis)  

names = [emoji.demojize(e) for e in emojis]
print(names)  

Output:

['😊', '🌞', '🎉']
[':smiling_face_with_smiling_eyes:', ':sun_with_face:', ':party_popper:']

Counting Emoji Usage

import emoji
from collections import Counter

def count_emojis(text):
    """Count emoji occurrences in text."""
    emojis = [char for char in text if char in emoji.EMOJI_DATA]
    return Counter(emojis)

text = "Love it! ❤️❤️❤️ Amazing! 🔥🔥"
counts = count_emojis(text)
print(counts)  

Preprocessing for NLP Pipelines

import emoji
import re

def preprocess_for_nlp(text, mode='convert'):
    """
    Preprocess text with emoji handling for NLP.
    
    Modes:
        - 'convert': Replace emojis with text descriptions
        - 'remove': Strip emojis entirely
        - 'separate': Move emoji descriptions to end
    """
    if mode == 'remove':
        return emoji.replace_emoji(text, replace='')
    
    elif mode == 'convert':
        converted = emoji.demojize(text, delimiters=(' ', ' '))
        converted = converted.replace('_', ' ')
        return ' '.join(converted.split())
    
    elif mode == 'separate':
        emojis = [emoji.demojize(c) for c in text if c in emoji.EMOJI_DATA]
        clean = emoji.replace_emoji(text, replace='')
        return f"{clean.strip()} [{', '.join(emojis)}]"
    
    return text

text = "This movie is 🔥🔥🔥! Best ever 👍"

print(preprocess_for_nlp(text, 'convert'))

print(preprocess_for_nlp(text, 'remove'))

print(preprocess_for_nlp(text, 'separate'))

Handling Emoji Sequences

Some emojis are composed of multiple Unicode characters:

import emoji

text = "Hello 👨‍👩‍👧‍👦 and 🏳️‍🌈"

converted = emoji.demojize(text)
print(converted)

Summary

Goal Library Function
Shortcodes (:smile:) emoji emoji.demojize()
Restore emojis emoji emoji.emojize()
Full descriptions demoji demoji.replace_with_desc()
Find all emojis demoji demoji.findall()
Remove emojis emoji emoji.replace_emoji()
Navigation