tags:

views:

109

answers:

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

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

+8  A: 

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).

thrope
Well, i'm just doing "print thelist" . How do I do that for the variable? thelist.__str__()?
TIMEX
print [str(x) for x in l]
Simon
@alex - just print the list comprehension in my answer (I added print now) - probably better to use encode though
thrope
@Simon - yep str(x) is much clearer!
thrope
If you have characters (eg. u'süden') in your string that cannot be encoded as ascii x.__str__() or str(x) will not work (UnicodeEncodeError). The right way is print [x.encode(encoding) for x in l]
Peter Hoffmann
+4  A: 

Python contains string classes for both unicode strings and regular strings. The u before a string 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'>

See http://docs.python.org/library/stdtypes.html#sequence-types-str-unicode-list-tuple-buffer-xrange for more information about the string types available in Python.

Pär Wieslander