print python emoji as unicode string
Asked Answered
I

8

26

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 πŸ˜„"

Irina answered 7/9, 2014 at 5:0 Comment(0)
I
15

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')
Irina answered 7/9, 2014 at 5:31 Comment(4)
can you please mention package name for these function in python . – Bordeaux
unicode is built-in and .encode() is string function - no need to import any packages – Spelunker
@vijayathithya bytes.decode(b'\xf0\x9f\x98\x84', 'utf8') might be the Python 3 version of what the unicode call above is alluding to. if you know you have a bytes object, you can call decode directly on it to get a (unicode) str object, e.g. b'\xf0\x9f\x98\x84'.decode('utf8') – Steiger
@SamMason yeh mate!! – Aldo
L
25

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/

Lauber answered 23/10, 2018 at 16:13 Comment(3)
This doesn't work for the "thumbs up" emoji for some reason? – Nam
Good catch @JonathanSimon. It turns out that what actually should be used are 'Name Aliases' and not the 'short names'. Updated the answer accordingly. For thumbs up the name alias is 'thumbs up sign'. So, print('\N{thumbs up sign}') works fine. – Lauber
The link is 404. This one might be more related. unicode.org/Public/14.0.0/ucd/emoji/emoji-data.txt – Voltammeter
M
19
>>> print u'\U0001f604'.encode('unicode-escape')
\U0001f604
Mufti answered 7/9, 2014 at 5:6 Comment(0)
I
15

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')
Irina answered 7/9, 2014 at 5:31 Comment(4)
can you please mention package name for these function in python . – Bordeaux
unicode is built-in and .encode() is string function - no need to import any packages – Spelunker
@vijayathithya bytes.decode(b'\xf0\x9f\x98\x84', 'utf8') might be the Python 3 version of what the unicode call above is alluding to. if you know you have a bytes object, you can call decode directly on it to get a (unicode) str object, e.g. b'\xf0\x9f\x98\x84'.decode('utf8') – Steiger
@SamMason yeh mate!! – Aldo
E
13

Just add

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

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

Elagabalus answered 4/6, 2017 at 18:56 Comment(0)
A
5

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

Code

# -*- 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!')

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 πŸ˜„ 
Amide answered 4/5, 2019 at 1:36 Comment(0)
I
3

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'
print(disc)
# πŸ“€
Izak answered 10/10, 2021 at 11:55 Comment(0)
W
2

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

>>> print '%r' % u'\U0001f604'
u'\U0001f604'
Webber answered 7/9, 2014 at 17:5 Comment(0)
C
2

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 πŸ˜„

Clientele answered 28/7, 2021 at 15:11 Comment(0)

© 2022 - 2024 β€” McMap. All rights reserved.