Why do my hobbies have a ‘u’ before them when I print them?



Why do my hobbies have a ‘u’ before them when I print them?


This happens because raw_input() returns a unicode string, and that’s all the u means. If you’re seeing some output like this: [u'knitting', u'biking', u'coding'], that’s totally normal. Python is intentionally letting you know that the output is unicode.
To “fix” this, we can convert our hobbies to strings as we append them, like this: hobbies.append(str(hobby)).

FAQ: Learn Python- Loops - For your hobbies

What is unicode? Does it mean anything special that I need to avoid or be careful?


python has documentation about unicode:


although maybe not the easier read, certainly very useful.


Okay, so If I type Cyrillic symbols for my raw_input I receive: (u, u*unicode for cyrillic symbol).
Well my question is how can I work in python with that symbols?


Are we talking data inputs or code symbols?


Data inputs. I guess code symbols must be english characters.


The language interpreter is written in English being as it goes back to the very beginning of programming which had its biggest gains in the English speaking western world. It would not make any sense to have multiple symbols for each instruction. As for data, most or all programming languages can work with Unicode.

I’m not sure (will need to test) whether we can use Unicode in variable and function names, but one could reason there should be nothing stopping us doing that. LIkewise we can port the English keywords to our native language…

>>> impresión = print
>>> impresión("Esta es una frase en español")
Esta es una frase en español

That answers that question for us. Note that impresión is not a function, proper, but a reference to the print function. print() is still doing the work, just under a different name.


As far as I can tell, even older Python works with unicode (foreign characters, etc) just fine.

The problem here is very likely that when you print a list, like in print hobbies, the list is calling the __repr__ function on each item, instead of __str__ or __format__. The reason it responds with a u'\u4f60\u597d\u5417' kind of string is that if you didn’t trust everything to support unicode correctly, that string would always be able to recreate the original string.

This seems to be fixed in Python 3, but for now, if you try something like for h in hobbies: print h to loop through all of the items, it should print all the unicode the way you expect.


Is there a reason that why I initialize hobby outside of my for loop in only ask for user input on the first loop and appends that response to the hobbies list 3 times?

I would imagine each time it would run hobby it would ask for the user input.


Console printed this:

What is a hobby you have? snowboarding
[u’snowboarding’, u’snowboarding’, u’snowboarding’]


only code inside the loop execute multiple times, so if you want to prompt the user for input multiple times, ensure the code responsible for this is inside the loop


I have the same issue and I did put the raw_input inside the loop. (and I had the similar issue in a step before.

My code:

hobbies = []

# Add your code below!

for num in range(3):
  hobby =  raw_input("Tell me one of your favorite hobbies: ")

print hobbies

console output:

Tell me one of your favorite hobbies: football
Tell me one of your favorite hobbies: Tell me one of your favorite hobbies: [u’football’, u’football’, u’football’]


That makes it a different issue altogether.
Assuming your description is accurate and I’m managing to read it accurately, then codecademy isn’t dealing with io streams correctly there.
I suggest ditching taking input, and/or doing it locally instead.

Rather than hard-coding 3, it would be nicer if the program read until the end of input. This is a little bit tricky because end of input won’t show up like regular text. The easiest way to go about it is to treat standard input like a file (from your program’s perspective, it is a file)

import sys

for line in sys.stdin:

There’s also a neat module which checks the arguments provided to the program for a file name, and if so it reads from that file, otherwise it reads from stdin:

import fileinput

for line in fileinput.input():

Beware of line terminators at the end. You may need/want to strip the lines before using them.
raw_input is just a helper for sys.stdin, same for print and sys.stdout


Not sure why we use ‘range(3)’?

How come it isn’t ‘range(2)’ since we are prompting the user to enter something 3 times i.e. for each index of range(2) (0,1,2)?


range gives values lesser then (<) stop value, not lesser then or equal to


I want to ask about the str().

Why hobbies.append(str(hobby))?

why does
-> str(hobby) = raw_input…
-> print str(hobby)

not work?


this won’t work:

str(hobby) = raw_input…

you could do:

hobby = str(raw_input())

casting the result of raw_input to a string