Convert Text to Emoji and vice versa in Python

This Python tutorial to show you how to convert text to emoji and emoji to text in Python using the emoji package.

Install emoji package

Installing the emoji package using the following command.

pip install emoji

Convert Text to Emoji in Python

The following Python program we convert string which contains emoji names or emoji shortcodes into a string with contains unicode characters of emoji using the emoji.emojize() method.

import emoji

str = emoji.emojize('Make with :red_heart:  from toricode.com')

print(str)
The output as below.
Make with ❀️  from toricode.com

Convert Emoji to Text in Python

In the following Python program, we use the emoji.demojize() method to convert a string contains emoji unicode characters into a string contains emoji names or emoji shortcodes.

import emoji

str = emoji.demojize('πŸ˜€ πŸ˜ƒ πŸ˜„ 😁 πŸ˜† πŸ˜… πŸ˜‚ 🀣 πŸ₯² 😊 πŸ˜‡ πŸ™‚ πŸ™ƒ')

print(str)
The output as below.
:grinning_face: :grinning_face_with_big_eyes: :grinning_face_with_smiling_eyes: :beaming_face_with_smiling_eyes: :grinning_squinting_face: :grinning_face_with_sweat: :face_with_tears_of_joy: :rolling_on_the_floor_laughing: :smiling_face_with_tear: :smiling_face_with_smiling_eyes: :smiling_face_with_halo: :slightly_smiling_face: :upside-down_face:

Check if a string is an Emoji in Python

The following Python program to show you how to use the emoji.is_emoji() method to check whether a string is an emoji or not.

import emoji

result1 = emoji.is_emoji('πŸ‘')
result2 = emoji.is_emoji('HelloπŸ‘')
result3 = emoji.is_emoji('Hello')

print(result1)
print(result2)
print(result3)
The output as below.
True
False
False

Happy Coding 😊

Tags: