print python emoji as unicode string

print python emoji as unicode string

Problem Description:

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 'xf0x9fx98x84'. 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 😄"

Solution – 1

>>> print u'U0001f604'.encode('unicode-escape')

Solution – 2

I found the solution to the problem.

I wrote the following code:

#convert to unicode
teststring = unicode(teststring, 'utf-8')

#encode it with string escape
teststring = teststring.encode('unicode_escape')

Solution – 3

If this is for debugging purposes, you could use %r as the format specifier.

>>> print '%r' % u'U0001f604'

Solution – 4

Just add

# -*- coding: UTF-8 -*-

into your code and you will be able to print Unicode characters

Solution – 5

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:

Or the Folder for everything here:

Solution – 6

This code might help you to see how you can simply print an emoji:


# -*- coding: UTF-8 -*-
import re

# string = 'Anything else that you wish to match, except URLs'
string = 'Anything else that you wish to match'
matches ='^(((?!http|https).)+)$', string)
if matches:
    print( " is a match 😄 ")
    print('🙀 Sorry! No matches! Something is not right!')

Output for string with URL

🙀 Sorry! No matches! Something is not right!

Output for string without URL

Anything else that you wish to match is a match 😄 

Solution – 7

I tried this and it worked perfectly fine


So you will get the output


So just put u at beginning of your string.
Hope it helps 😄

Solution – 8

Print Unicode with U+ prefix

I was looking a this usefull list of emoji Here and also This one

A part of just copy any emoji like –> 📀 I wanted to map in my code some of this emojy but using the code provided.
In the example of the link it would be like -> U+1F4C0
Now if you find the code formatted in this way, what you need to do is the following:

  1. Replace "+" with "000"
  2. Prefix the Unicode with ""

The result should be :

dict = 'U0001F4C0'
# 📀
Rate this post
We use cookies in order to give you the best possible experience on our website. By continuing to use this site, you agree to our use of cookies.