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

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

Is there any way to print it without the โ€œuโ€ in front of it?

+4
source share
2 answers

What you see is a representation of __repr__() a Unicode string that includes u to make it understandable. If you do not want you to be able to print the object (using __str__ ) - this works for me:

 print [str(x) for x in l] 

It is probably best to read python unicode and encode using the proper Unicode codec:

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

[edit]: to clarify the reprint and why u exists, the goal is to provide a convenient string representation "to return a string that will give an object with the same value when passed to eval ()", i.e. you can copy and paste the printed output and get the same object (a list of strings in Unicode).

+9
source

Python contains string classes for both unicode strings and regular strings. U in front of the line indicates that it is a Unicode string.

 >>> mystrings = [u'Iphones', u'dont', u'receieve', u'messages'] >>> [str(s) for s in mystrings] ['Iphones', 'dont', 'receieve', 'messages'] >>> type(u'Iphones') <type 'unicode'> >>> type('Iphones') <type 'str'> 

For more information on the types of strings available in Python, see http://docs.python.org/library/stdtypes.html#sequence-types-str-unicode-list-tuple-buffer-xrange .

+4
source

All Articles