8

'u' before elements in printed list? I didn't type u in my code.

hobbies = []

#prompt user three times for hobbies
for i in range(3):
    hobby = raw_input('Enter a hobby:')
    hobbies.append(hobby)

#print list stored in hobbies
print hobbies

When I run this, it prints the list but it is formatted like this:

Enter a hobby: Painting
Enter a hobby: Stargazing
Enter a hobby: Reading
[u'Painting', u'Stargazing', u'Reading']
None

Where did those 'u' come from before each of the elements of the list?

PM 2Ring
  • 54,345
  • 6
  • 82
  • 182
  • `u` represents a unicode string. Check this: http://stackoverflow.com/questions/4855645/how-to-remove-the-u-as-in-unicode-in-usomestring – karthikr Oct 03 '13 at 23:33
  • Try reading this http://docs.python.org/2/howto/unicode.html – Joel Green Oct 03 '13 at 23:33
  • 1
    The `u` is no more a part of the string itself than the `'` characters are. The actual string is `Painting`, not `u'Painting'` or `'Painting'`. – abarnert Oct 03 '13 at 23:47
  • possible duplicate of [python convert unicode to string](http://stackoverflow.com/questions/16819848/python-convert-unicode-to-string) – kojiro Oct 04 '13 at 00:11
  • @kojiro my question was more on 'why' there is 'u'.. –  Oct 04 '13 at 00:19
  • @Lotus92 Having carefully looked at the questions, I still am of the opinion they are "the same" question. They're phrased differently, but I think they come from the same understanding of Python and `repr`s. – kojiro Oct 04 '13 at 00:22

4 Answers4

13

I think what you're actually surprised by here is that printing a single string doesn't do the same thing as printing a list of strings—and this is true whether they're Unicode or not:

>>> hobby1 = u'Dizziness'
>>> hobby2 = u'Vértigo'
>>> hobbies = [hobby1, hobby2]
>>> print hobby1
Dizziness
>>> print hobbies
[u'Dizziness', u'V\xe9rtigo']

Even without the u, you've got those extra quotes, not to mention that backslash escape. And if you try the same thing with str byte strings instead of unicode strings, you'll still have the quotes and escapes (plus you might have mojibake characters if your source file and your terminal have different encodings… but forget that part).


In Python, every object can have two different representations: the end-user-friendly representation, str, and the programmer-friendly representation, repr. For byte strings, those representations are Painting and 'Painting', respectively. And for Unicode strings, they're Painting and u'Painting'.

The print statement uses the str, so print hobby1 prints out Painting, with no quotes (or u, if it's Unicode).

However, the str of a list uses the repr of each of its elements, not the str. So, when you print hobbies, each element has quotes around it (and a u if it's Unicode).

This may seem weird at first, but it's an intentional design decision, and it makes sense once you get used to it. And it would be ambiguous to print out [foo, bar, baz]—is that a list of three strings, or a list of two strings, one of which has a comma in the middle of it? But, more importantly, a list is already not a user-friendly thing, no matter how you print it out. My hobbies are [Painting, Stargazing] would look just as ugly as My hobbies are ['Painting', 'Stargazing']. When you want to show a list to an end-user, you always want to format it explicitly in some way that makes sense.

Often, what you want is as simple as this:

>>> print 'Hobbies:', ', '.join(hobbies)
Hobbies: Painting, Stargazing

Or, for Unicode strings:

>>> print u'Hobbies:', u', '.join(hobbies)
Hobbies: Painting, Stargazing
abarnert
  • 354,177
  • 51
  • 601
  • 671
  • You must be the fastest typist ever! The last five questions I looked at all had long answers like this one! – SethMMorton Oct 04 '13 at 01:11
  • Perfect. Just the answer i was looking for! –  Oct 04 '13 at 01:37
  • @SethMMorton: I am a pretty fast typist. But also, whenever I have to wait for a compile or a test to run or something else that takes long enough to be annoying, but not long enough to answer a new question or play a quick game or take a break, I tend to go back and re-edit the answer sitting in front of me. If SO showed complete edit history instead of collapsed, you'd be surprised… – abarnert Oct 04 '13 at 01:37
7

The 'u' is not part of the string, but indicates that the string is a unicode string.

blin00
  • 71
  • 1
  • 3
6

You're not printing the strings, you're printing the representation of the list holding the strings.

for hobby in hobbies:
  print hobby
Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358
0

If you want to convert the unicode to string. You can simply use str(unicodedString) or unicode(normalString) for the other way conversion

Code

hobbies = []

#prompt user three times for hobbies
for i in range(3):
    hobby = raw_input('Enter a hobby:')
    # converting the normal string to unicode
    hobbies.append(unicode(hobby))

# Printing the unicoded string
print("Unicoded string")
print(hobbies)
hobbies = [str(items) for items in hobbies]

# Printing the converted string
print("Normal string from unicoded string")
print(hobbies)

Output

Enter a hobby:test1
Enter a hobby:Test2
Enter a hobby:Test3

Unicoded string
[u'test1', u'Test2', u'Test3']

Normal string from unicoded string
['test1', 'Test2', 'Test3']