Programmatically Detecting, Removing, and Converting Emojis in Python
Managing emojis programmatically is a common requirement in data cleaning, sentiment analysis, and Natural Language Processing (NLP). Unlike standard ASCII characters, emojis are complex Unicode characters that can span multiple code points (such as skin tone modifiers or Zero Width Joiner sequences). Simple string searches or basic regular expressions often fail to match them correctly.
This guide provides a comprehensive walkthrough of emoji detection, extraction, stripping, conversion to descriptive shortcodes, and rendering using Python.
Detecting and Extracting Emojis in Textβ
The most robust way to interact with emojis in Python is using the third-party emoji library, which aligns with the latest Unicode standards.
1. Installationβ
pip install emoji
2. Checking for Emojisβ
To verify if a string contains any emoji characters, use emoji.emoji_count():
import emoji
text_with_emoji = "Python is fun! ππ»π₯"
text_clean = "This text is clean."
# Check if emoji count is greater than 0
contains_emoji = emoji.emoji_count(text_with_emoji) > 0
is_clean = emoji.emoji_count(text_clean) > 0
print(f"Contains emoji: {contains_emoji}") # True
print(f"Contains emoji: {is_clean}") # False
3. Extracting All Emojisβ
To retrieve a list of every emoji in a block of text, including their precise character positions and individual shortcodes:
import emoji
sample_text = "I love this library! ππ½ The astronaut π©βπ is cool."
emojis_found = emoji.emoji_list(sample_text)
print(emojis_found)
# Output:
# [{'match_start': 22, 'match_end': 25, 'emoji': 'ππ½'},
# {'match_start': 41, 'match_end': 44, 'emoji': 'π©βπ'}]
# To extract just the emoji characters
emoji_chars = [match['emoji'] for match in emojis_found]
print(emoji_chars) # ['ππ½', 'π©βπ']
4. Checking if a Single Character is an Emojiβ
import emoji
print(emoji.is_emoji('π©βπ»')) # True (handles multi-code-point composite emojis)
print(emoji.is_emoji('A')) # False
5. Manual Unicode Ranges (Legacy/No-Dependency Method)β
If you cannot install third-party packages, you can use regular expressions to match major Unicode emoji blocks.
Note: This approach is not recommended for production because it frequently misses newer emojis and does not support composite sequences (like skin tones).
import re
EMOJI_REGEX = re.compile(
"["
"\U0001F600-\U0001F64F" # Emoticons
"\U0001F300-\U0001F5FF" # Miscellaneous Symbols and Pictographs
"\U0001F680-\U0001F6FF" # Transport and Map Symbols
"]+", flags=re.UNICODE
)
text = "Simple happy face π"
print(bool(EMOJI_REGEX.search(text))) # True
Converting Emojis to Text (Demojization)β
For machine learning models, transforming emojis into descriptive text shortcodes preserves the underlying sentiment while converting the data into a standard string representation.
import emoji
text_data = "I love this library! ππ½ The astronaut π©βπ is cool. β€οΈ"
# 1. Default demojization (includes colons)
shortcode_text = emoji.demojize(text_data)
print(shortcode_text)
# Output: I love this library! :thumbs_up_medium_skin_tone: The astronaut :woman_astronaut: is cool. :red_heart:
# 2. Custom delimiters (e.g. spaces instead of colons)
custom_text = emoji.demojize(text_data, delimiters=(" ", " "))
print(custom_text)
# Output: I love this library! thumbs_up_medium_skin_tone The astronaut woman_astronaut is cool. red_heart
Removing or Replacing Emojisβ
1. Direct Strippingβ
Use emoji.replace_emoji() to remove emojis completely:
import emoji
text_data = "Python is great! ππ»π₯"
# Strip all emojis
text_removed = emoji.replace_emoji(text_data, replace='')
print(text_removed) # Output: Python is great!
2. Replacing with Placeholder Tokensβ
import emoji
# Replace with a custom string placeholder
text_placeholder = emoji.replace_emoji(text_data, replace='[EMOJI]')
print(text_placeholder) # Output: Python is great! [EMOJI][EMOJI][EMOJI]
3. Alternative: clean-text Libraryβ
For full text normalization (removing emojis, emails, phone numbers, and digits simultaneously), you can use the clean-text package:
from cleantext import clean
mixed_data = "Check out my new project! π Contact me at user@example.com."
cleaned_text = clean(mixed_data, no_emoji=True, no_emails=True)
print(cleaned_text) # Output: check out my new project contact me at
Converting Text to Emojis (Emojization)β
1. Converting Standard Shortcodesβ
To render Unicode emojis from text shortcodes, use emoji.emojize():
import emoji
text_to_convert = "Python is :thumbs_up: and I :red_heart: it."
converted_text = emoji.emojize(text_to_convert)
print(converted_text) # Output: Python is π and I β€οΈ it.
2. Custom Word-to-Emoji Mappingsβ
For custom domain-specific replacements, you can implement a standard Python dictionary mapping:
custom_emoji_map = {
"happy": "π",
"smile": "π",
"python": "π",
"cool": "π"
}
def text_to_emoji(text: str, emoji_map: dict) -> str:
words = text.split()
output_words = [emoji_map.get(word.lower(), word) for word in words]
return " ".join(output_words)
my_text = "I am very happy and I think python is cool"
print(text_to_emoji(my_text, custom_emoji_map))
# Output: I am very π and I think π is π
Sourcesβ
- [1] PyPI Project Page: emoji Library
- [2] Tutorialspoint Guide: Convert Emoji into Text in Python
- [3] GeeksforGeeks Article: Introduction to emoji Module in Python
- [4] StackOverflow discussion: How to replace emoji with words in a text string
- [5] Project Gurukul Reference: Python Emoji to Text Converter
