10/19, why is the console printing out extra stuff?


#1

So I have

hobbies = []

for i in range(3):
  hobby = raw_input("Gimme hobby: ")
  hobbies.append(hobby)
print hobbies

And if I put in, say,

Gimme hobby: cooking
Gimme hobby: shopping
Gimme hobby: fishing

It outputs

[u'cooking', u'shopping', u'fishing']

But I didn’t put the u or the quotes there. Is something happening behind the scenes I am not aware of?


#2

yes, the result of raw_input is stored as unicode:

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

then i don’t have to tell you what the u stands for


#3

There HAS to be something going on behind the scene, because you haven’t specified how that value should be printed. (What output did you expect?)


#4

Thank you–a little confused still but I’ll accept it as is for now.


#5

The output I expected was:

[cooking, shopping, fishing]

How would I remove the u"string" format? For the sake of making it pretty.


#6

[cooking, shopping, fishing] isn’t a value literal, so that won’t get printed. If you have such variables in code, then yeah you can write that, but this doesn’t describe a value by itself.

You don’t get rid of quotes any more than you get rid of the square brackets, commas, spaces in what you wrote. If you want to print it some way then what you do is you create a string based on the values that you want to display, and then you print that string

'I\'m {}, and I have a {} {}'.format('bob', 'red', 'ball')

The u and the quotes isn’t part of the data. If it was part of the data, then yes, you would want to clean up the data.


#7

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.