Emoji for Python
The `emoji` library provides comprehensive support for handling Unicode emojis in Python, allowing conversion between emoji characters and their shortcodes (e.g., ๐ to :thumbs_up:). It supports the entire set of Emoji codes defined by the Unicode consortium and various languages. The library is actively maintained, with version 2.15.0 being the latest, and typically releases updates to keep up with Unicode emoji standards.
Warnings
- breaking Version 2.0.0 introduced breaking changes concerning non-English short codes. The names of emoji in non-English languages were updated to Unicode CLDR version 41, meaning some previously stored non-English :short-code-emoji: might no longer work or be ignored by `emojize()`.
- breaking The `get_emoji_regexp()` function was removed in version 2.0.0. The internal mechanism for scanning emojis no longer relies on regular expressions due to performance and accuracy issues with complex Unicode emoji sequences.
- breaking Support for Python 2.7, 3.4, and 3.5 was removed in version 2.5.0. The library now requires Python 3.8 or newer.
- gotcha Inconsistent emoji rendering across platforms and terminals. Emojis are Unicode characters, but their visual representation depends on the font support and rendering capabilities of the environment where the Python script is run.
- gotcha Potential `UnicodeEncodeError` when handling emoji. This often occurs when text containing emojis is processed or written to a file/output stream using an encoding that does not support the full range of Unicode characters, such as ASCII instead of UTF-8.
- gotcha Aliases (`language='alias'`) for `emojize()` and `demojize()` are specific to English. Using `language='alias'` with other languages might not produce expected results or could lead to warnings in older versions.
Install
-
pip install emoji
Imports
- emoji
import emoji
- emojize
import emoji text_with_emoji = emoji.emojize('Python is :thumbs_up:') - demojize
import emoji text_with_shortcodes = emoji.demojize('Python is ๐') - analyze
import emoji emoji_data = list(emoji.analyze('Python is ๐')) - replace_emoji
import emoji clean_text = emoji.replace_emoji('Python is ๐', replace='')
Quickstart
import emoji
# Convert shortcodes to emoji
emojified_text = emoji.emojize('Python is fun :red_heart: :snake:')
print(f"Emojified: {emojified_text}")
# Convert emoji to shortcodes
demojified_text = emoji.demojize('Python is fun โค๏ธ๐')
print(f"Demojified: {demojified_text}")
# Analyze text for emojis
emojis_found = list(emoji.analyze('Hello ๐ world ๐'))
print(f"Emojis found: {emojis_found}")
# Replace emojis with a custom string
text_without_emojis = emoji.replace_emoji('Hello ๐ world ๐', replace='[emoji]')
print(f"Text without emojis: {text_without_emojis}")