How to remove emojis from a string in Python?

Sometimes, we want to remove emojis from a string in Python.

In this article, we’ll look at how to remove emojis from a string in Python.

How to remove emojis from a string in Python?

To remove emojis from a string in Python, we can create a regex that matches a list of emojis.

For instance, we write

import re

text = u'This is a smiley face U0001f602'
print(text) # with emoji

def de_emojify(text):
    regex_pattern = re.compile(pattern = "["
        u"U0001F600-U0001F64F" 
        u"U0001F300-U0001F5FF"
        u"U0001F680-U0001F6FF"
        u"U0001F1E0-U0001F1FF"
                           "]+", flags = re.UNICODE)
    return regex_pattern.sub(r'', text)

print(de_emojify(text))

to call re.compile with pattern set to a string that matches the character code ranges for emojis.

U0001F600-U0001F64F is the code range for emoticons.

U0001F300-U0001F5FF is the range for symbols and pictographs.

U0001F680-U0001F6FF" is the range for transport and map symbols.

U0001F1E0-U0001F1FF is the range for flag emojis in iOS.

Then we call regex_pattern.sub to replace the emojis in text with empty strings.

Conclusion

To remove emojis from a string in Python, we can create a regex that matches a list of emojis.