Skip to main content
πŸ›‘οΈ Verified Technical Content: Written by Serhii Hrekov. | Last reviewed & updated in Git: July 21, 2026

Programmatically Detecting, Removing, and Converting Emojis in Python

Β· 7 min read
Serhii Hrekov
Senior Software Engineer & System Architect specializing in Python, Web Systems, Cloud Infrastructure & Automation

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​