I've been trying to output '😄'
as '\U0001f604'
instead of the smiley, but it doesn't seem to work.
I tried using repr()
but it gives me this '\xf0\x9f\x98\x84'
. Currently it outputs the smiley which is not what I wanted. encode('unicode_escape')
gives me a UnicodeDecodeError
.
The smiley was passed as a string to a class method in python. i.e. "I am happy 😄"
Emojis also have a CLDR short name, which can also be used. From the list of unicodes, replace “+” with “000”. For example – “U+1F600” will become “U0001F600” and prefix the unicode with “\” and print it.
You have two options to create Unicode string in Python. Either use decode() , or create a new Unicode string with UTF-8 encoding by unicode(). The unicode() method is unicode(string[, encoding, errors]) , its arguments should be 8-bit strings.
>>> print u'\U0001f604'.encode('unicode-escape')
\U0001f604
Another solution is to use the name aliases and print them using the string literal \N
print('\N{grinning face with smiling eyes}')
Current list of name aliases can be found at https://www.unicode.org/Public/14.0.0/ucd/NamesList-14.0.0d1.txt
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With