Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is it when I print something, there is always a unicode next to it? (Python)

Tags:

python

[u'Iphones', u'dont', u'receieve', u'messages']

Is there a way to print it without the "u" in front of it?

like image 589
TIMEX Avatar asked Dec 13 '22 01:12

TIMEX


1 Answers

What you are seeing is the __repr__() representation of the unicode string which includes the u to make it clear. If you don't want the u you could print the object (using __str__) - this works for me:

print [str(x) for x in l]

Probably better is to read up on python unicode and encode using the particular unicode codec you want:

print [x.encode() for x in l]

[edit]: to clarify repr and why the u is there - the goal of repr is to provide a convenient string representation, "to return a string that would yield an object with the same value when passed to eval()". Ie you can copy and paste the printed output and get the same object (list of unicode strings).

like image 160
robince Avatar answered Apr 12 '23 22:04

robince