10/19 - lowercase "u" inserted into list?


#1

Hi,

This is my code for this exercise (which works, apparently: allowing me to progress to the next exercise). However a lowercase letter "u" is inserted in front of all hobby entries. Anyone know why?
Cheers for any help..

hobbies = []
    
for i in range(3):
    hobby = raw_input("What is a hobby of yours?: ")
    hobbies.append(hobby)
print hobbies

output is:

What is a hobby of yours?:  tennis
What is a hobby of yours?:  running
What is a hobby of yours?:  cycling
[u'tennis', u'running', u'cycling']
None

Note the "u" added in front of every hobby in the list...! Why is it?


#2

I also encountered this and forgot to follow up on it.

google: raw_input prepend u

for example, if you do:

print hobbies[1]

you'll get the item in the list sans u' '


#3

But just one item. What you really want to do is what is suggested on SO.

hobbies = []

for i in range(3):
    hobby = raw_input("Enter a hobby: ")
    hobby = hobby.encode('utf-8')
    hobbies.append(hobby)

print hobbies

10/19
#4

Thanks kcamluap and dvandekar.

  • I see it simply means its a unicode string and doesn't affect the list contents in any other way..
    (although its currently above me to understand the significance of either way at this stage!)

Thanks also for the suggested fix dvandekar: looks good, I'll try that.


#5

dvandekar, I tried the fix you suggested (hobby = hobby.encode('utf-8') it didn't make any difference. the printout still had the lowercase u's in front of each entry. It's like the program ignored that line.


#6

noeman5: dvandekars suggestion works for me - here is my complete code so you can try it if you like:

hobbies = []

# Add your code below!
for i in range(3):
    hobby = raw_input("What is a hobby of yours?: ")
    hobby = hobby.encode('utf-8')
    hobbies.append(hobby)
print hobbies

#7

@noeman5 if you share your code we can try to see why. It worked for me and it looks like also @mrsteel.


#8

Hi Noeman,

the lowercase 'u' is appearing as it's a unicode sting.

If you put 'str' before raw_input it should hopefully work!

So it should look like this:

hobby=str(raw_input("What is your hobby?"))
hobbies.append(hobby)

I hope that helps! :smile:


10. For your hobbies
#9

I tried with the hobby.encode('utf-8') and it didn't make a difference:

hobbies = []

# Add your code below!
for hobbies in range(3):
    hobby = str(raw_input("please enter"))
    hotty = hobby.encode ('utf-8')
    hobbies.append(hobby)
    
print hobby

I get the error: Traceback (most recent call last):
File "python", line 7, in
AttributeError: 'int' object has no attribute 'append


#10

You have a number of issues going on. Check hotty should be hobby and I would avoid the naming conflict of using hobbies as the loop variable. Also you are printing the wrong variable.

hobbies = []

# Add your code below!
for i in range(3):
    hobby = str(raw_input("please enter"))
    hobby = hobby.encode ('utf-8')
    hobbies.append(hobby)
    
print hobbies