Strange output error in for loops


#1

I'm stuck in the for loop section of the exercise. I've run for loops several times but haven't come across such a strange error. Every time I run the below code, I get some strange elements in my output.

Enter your hobby: cricket
Enter your hobby: movies
Enter your hobby: computers
[u'cricket', u'movies', u'computers']
None

I've no clue where is this u' coming in the output as I don't seem to input it anywhere.

hobbies = []


# Add your code below!
for i in range(3):
    hobby = raw_input("Enter your hobby:")
    hobbies.append(hobby)
print hobbies    .

#2

Hi,

u denotes the string is of Unicode type. To convert it to regular string, just do:

hobby = raw_input("Enter your hobby:")
hobby = str(hobby) # gets rid of that u

#3

The u' stands for unicode. This is because those inputs are unicode strings. If you want to remove them, you should convert the strings to ascii by using hobby.decode('ascii')
Or as @gaurangtandon has said, str(hobby) also works.


#4

Does that mean that all string inputs by the user will default to unicode type?


#5

@nicoekkart AnswerConflictException :smile:


#6

It depends on your machine if I recall correctly. Where are you running it, because I'm not seeing the output that you reported?


#7

HP Elitebook.

OS: Windows 7 professional

But i don't remember these characters showing up in the previous sections as I am assuming I must have inputted some strings as part of the exercise process while working my way through the course.