Skip to main content

Сonvert text to emoji in Python guide

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

To convert text to emoji in Python, you can use the emoji module. This third-party library provides functions to replace specific text patterns, known as "shortcodes" or "aliases," with their corresponding Unicode emoji characters. This is the most straightforward and recommended way to add emojis to your text [1, 3].


1. Installation 💾

First, you need to install the emoji library using pip, Python's package installer. Open your terminal or command prompt and run the following command:

pip install emoji

2. Basic Text-to-Emoji Conversion (emojize) ⚙️

The primary function for this task is emoji.emojize(). It takes a string as input and replaces a predefined set of shortcodes with emojis. These shortcodes are human-readable descriptions enclosed in colons, like :thumbs_up: or :red_heart:.

import emoji

# Text with shortcodes
text_to_convert = "Python is :thumbs_up: and I :red_heart: it."

# Convert the shortcodes to emojis
converted_text = emoji.emojize(text_to_convert)

# Print the result
print(converted_text)
# Output: Python is 👍 and I ❤️ it.

The emoji library supports a wide range of shortcodes and aliases, covering the entire set of emojis defined by the Unicode Consortium [3]. You can find a complete list of shortcodes in the official documentation.

3. Creating a Custom Dictionary for Conversion 🛠️

While the emoji module is excellent for its built-in functionality, you might have custom words you want to replace with emojis. For this, you can create a simple dictionary that maps words to their corresponding emojis and then iterate through the text to perform the replacement.

# A custom dictionary mapping words to emojis
custom_emoji_map = {
"happy": "😃",
"smile": "😊",
"python": "🐍",
"cool": "😎"
}

def text_to_emoji(text, emoji_map):
# Split the text into words
words = text.split()

# Iterate through the words and replace them if a match is found
output_words = [emoji_map.get(word.lower(), word) for word in words]

# Join the words back into a sentence
return " ".join(output_words)

# Example usage
my_text = "I am very happy and I think python is cool"
emoji_text = text_to_emoji(my_text, custom_emoji_map)

print(emoji_text)
# Output: I am very 😃 and I think 🐍 is 😎

This approach gives you full control over the mapping, allowing you to define any word-to-emoji substitutions you need [2].

Summary and Recommendations

  • For standard emoji shortcodes: Use the emoji module's emojize() function. This is the most robust and hassle-free method, as it relies on a comprehensive, up-to-date library.
  • For custom word-to-emoji mappings: Create a dictionary and use a simple loop or list comprehension to replace the words. This provides maximum flexibility for your specific needs.

Sources

  1. Tutorialspoint. "Convert Emoji into Text in Python." https://www.tutorialspoint.com/python/convert_emoji_into_text_in_python.htm
  2. Wellsr. "Convert Emojis to Text and Text to Emojis in Python." https://wellsr.com/python/convert-text-to-emojis-and-vice-versa-in-python/
  3. PyPI. "emoji." https://pypi.org/project/emoji/
  4. GeeksforGeeks. "Introduction to emoji Module in Python." https://www.geeksforgeeks.org/python/introduction-to-emoji-module-in-python/

This video provides a practical, code-focused guide on how to turn words into emojis in Python, which is a great way to see these concepts in action. Turn Words into Emojis with Python!