In this Python tutorial, you will learn how to convert text to emoji and emoji to text in Python using the emoji package. Unlock the magic of emojis in Python! Explore this guide to seamlessly convert text to emojis and vice versa, adding a playful touch to your projects. ππ
Installing the emoji package using the following command.
pip install emoji
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 codequs.com')
print(str)
The output as below.
Make with β€οΈ from codequs.com
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:
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 π
#python