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


#1

Question

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

Answer

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
#2

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


#3

python has documentation about unicode:

https://docs.python.org/2/howto/unicode.html

although maybe not the easier read, certainly very useful.


#4

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?


#5

Are we talking data inputs or code symbols?


#6

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


#7

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.


#8

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.


#9

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.


#10

Console printed this:

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


#11

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