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

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

0

2 Answers 2

10

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

Sign up to request clarification or add additional context in comments.

3 Comments

Well, i'm just doing "print thelist" . How do I do that for the variable? thelist.__str__()?
@alex - just print the list comprehension in my answer (I added print now) - probably better to use encode though
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]
4

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.