print python emoji as unicode string

You Hock Tan picture You Hock Tan · Sep 7, 2014 · Viewed 63.4k times · Source

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 😄"

Answer

Ignacio Vazquez-Abrams picture Ignacio Vazquez-Abrams · Sep 7, 2014
>>> print u'\U0001f604'.encode('unicode-escape')
\U0001f604