Skip to main content

How to Convert Emojis to Text in Python

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! 💯"

# Convert emojis to text shortcodes
converted = emoji.demojize(text)
print(converted)
# Output: Python is :fire: and :snake: is awesome! :hundred_points:

# Convert shortcodes back to emojis
restored = emoji.emojize(converted)
print(restored)
# Output: Python is 🔥 and 🐍 is awesome! 💯

Output:

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

Customizing Delimiters

import emoji

text = "Great job! 👍✨"

# Use different delimiters
result = emoji.demojize(text, delimiters=(" [", "] "))
print(result)
# Output: Great job! [thumbs_up] [sparkles]

# No delimiters (just the name)
result = emoji.demojize(text, delimiters=(" ", " "))
print(result)
# Output: Great job! thumbs_up sparkles

Output:

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

Language Support

import emoji

text = "Hello 👋 World 🌍"

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

Output:

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

Multiple languages available: en, es, pt, it, fr, de, etc.

Using demoji for Descriptive Text

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

pip install demoji
import demoji

# Download emoji data (run once)
demoji.download_codes()

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

# Replace with full descriptions
clean_text = demoji.replace_with_desc(text, sep=" ")
print(clean_text)
# Output: I love this! red heart rocket So exciting! party popper

# Find all emojis and their descriptions
emoji_dict = demoji.findall(text)
print(emoji_dict)
# Output: {'❤️': 'red heart', '🚀': 'rocket', '🎉': 'party popper'}
When to Use Each Library
  • emoji: Best for shortcode format (:fire:), chat applications, and reversible conversions
  • demoji: Best for NLP/ML features where you need natural language descriptions

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 up extra spaces
clean = ' '.join(clean.split())
print(clean) # Output: Hello World!

Using demoji for Removal

import demoji

text = "Check this out! 🔥💯🚀"

# Remove all emojis
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)
# ['😊', '🌞', '🎉']

# Get emoji names
names = [emoji.demojize(e) for e in emojis]
print(names)
# [':smiling_face_with_smiling_eyes:', ':sun_with_face:', ':party_popper:']

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)
# Counter({'❤️': 3, '🔥': 2})

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':
# Convert to words without colons
converted = emoji.demojize(text, delimiters=(' ', ' '))
# Replace underscores with spaces
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'))
# Output: This movie is fire fire fire ! Best ever thumbs up

print(preprocess_for_nlp(text, 'remove'))
# Output: This movie is ! Best ever

print(preprocess_for_nlp(text, 'separate'))
# Output: This movie is ! Best ever [:fire:, :fire:, :fire:, :thumbs_up:]

Handling Emoji Sequences

Some emojis are composed of multiple Unicode characters:

import emoji

# Compound emojis (skin tones, gender, flags)
text = "Hello 👨‍👩‍👧‍👦 and 🏳️‍🌈"

converted = emoji.demojize(text)
print(converted)
# Output: Hello :family_man_woman_girl_boy: and :rainbow_flag:

Summary

GoalLibraryFunction
Shortcodes (:smile:)emojiemoji.demojize()
Restore emojisemojiemoji.emojize()
Full descriptionsdemojidemoji.replace_with_desc()
Find all emojisdemojidemoji.findall()
Remove emojisemojiemoji.replace_emoji()
Best Practice

Use emoji for general applications, chat platforms, and when you need reversible conversions. Use demoji for data science and NLP tasks where natural language descriptions improve feature extraction and model understanding.