Reputation: 1015
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 😄"
Upvotes: 28
Views: 95053
Reputation: 7665
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:
The result should be :
dict = '\U0001F4C0'
print(disc)
# 📀
Upvotes: 4
Reputation: 654
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://unicode.org/Public/15.0.0/ucd/UnicodeData-15.0.0d6.txt
Or the Folder for everything here: https://unicode.org/Public/
Upvotes: 26
Reputation: 31
I tried this and it worked perfectly fine
print(u'\U0001f604')
So you will get the output
😄
So just put u
at beginning of your string.
Hope it helps 😄
Upvotes: 2
Reputation: 27723
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 http://url.org'
string = 'Anything else that you wish to match'
matches = re.search(r'^(((?!http|https).)+)$', string)
if matches:
print(matches.group(1)+ " is a match 😄 ")
else:
print('🙀 Sorry! No matches! Something is not right!')
🙀 Sorry! No matches! Something is not right!
Anything else that you wish to match is a match 😄
Upvotes: 5
Reputation: 131
Just add
# -*- coding: UTF-8 -*-
into your code and you will be able to print Unicode characters
Upvotes: 13
Reputation: 1015
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')
Upvotes: 15
Reputation: 521
If this is for debugging purposes, you could use %r as the format specifier.
>>> print '%r' % u'\U0001f604'
u'\U0001f604'
Upvotes: 2
Reputation: 798636
>>> print u'\U0001f604'.encode('unicode-escape')
\U0001f604
Upvotes: 19