Skip to main content

Transform emojis back into text in Python Guide

· 5 min read
Serhii Hrekov
software engineer, creator, artist, programmer, projects founder

To transform emojis back into text in Python, you can use the emoji module, which is a powerful third-party library for handling emojis. Specifically, the demojize() function converts Unicode emoji characters into their human-readable shortcode text (e.g., 👍 becomes :thumbs_up:) [2].


The demojize() Method: A Step-by-Step Guide ⚙️

The most reliable way to convert emojis to text is by using the demojize() function. It provides a consistent, standardized output based on the Unicode Consortium's emoji data.

Step 1: Installation 💾

First, make sure you have the emoji library installed. If you don't, you can install it using pip [1].

pip install emoji

Step 2: Basic demojize() Usage 📝

The demojize() function takes a string containing emojis as input and returns the same string with the emojis replaced by their shortcodes.

import emoji

# A string with emojis
emoji_text = "Python is great 👍, I ❤️ it."

# Convert the emojis back to text
text_with_shortcodes = emoji.demojize(emoji_text)

print(text_with_shortcodes)
# Output: Python is great :thumbs_up:, I :red_heart: it.

Step 3: Customizing the Output 🛠️

You can customize the output of demojize() by using the delimiters parameter. For example, if you don't want the shortcodes to be enclosed in colons, you can change the delimiters [3].

import emoji

emoji_text = "I am so happy 😊."

# Convert emojis without colons
text_without_colons = emoji.demojize(emoji_text, delimiters=("", ""))

print(text_without_colons)
# Output: I am so happy smiling_face_with_smiling_eyes.

A Simple Text-to-Emoji App 💻

Here's a simple command-line application that takes text with emojis as input and converts it to a string with emoji shortcodes. It provides a practical example of how to use the demojize() function.

import emoji

def de_emoji_app():
"""
A simple command-line application to convert emojis to text shortcodes.
"""
print("Welcome to the Emoji to Text Converter!")
print("Type your text with emojis and I'll convert them.")
print("Press Ctrl+C or type 'exit' to quit.")

while True:
try:
# Get user input
user_input = input("Enter text: ")

# Exit condition
if user_input.lower() == 'exit':
break

# Convert emojis to text
demojized_text = emoji.demojize(user_input)

# Print the result
print(f"Original Text: {user_input}")
print(f"Converted Text: {demojized_text}\n")

except KeyboardInterrupt:
print("\nExiting the application.")
break

except Exception as e:
print(f"An error occurred: {e}\n")

if __name__ == "__main__":
de_emoji_app()

Sources

  1. Tutorialspoint. "Convert Emoji into Text in Python." https://www.tutorialspoint.com/python/convert_emoji_into_text_in_python.htm
  2. GeeksforGeeks. "Introduction to emoji Module in Python." https://www.geeksforgeeks.org/python/introduction-to-emoji-module-in-python/
  3. Stack Overflow. "How to replace emoji to word in a text?" https://stackoverflow.com/questions/57580288/how-to-replace-emoji-to-word-in-a-text
  4. PyPI. "emoji." https://pypi.org/project/emoji/
  5. Project Gurukul. "Python Emoji to Text Converter." https://projectgurukul.org/python-emoji-to-text-converter/

This video provides a simple, code-based guide on converting emojis to text in Python, demonstrating the core concepts discussed here. Convert Emoji To Text in Python