Can someone explain why the str() is needed since the raw_input function returns as string already?

hobbies = []

# Add your code below!
for user_hobby in range(3):
    user_hobby = str(raw_input("What is your hobby: "))
    print hobbies


Slight modification to your code:

hobbies = []
for user_hobby in range(3):
    user_hobby = raw_input("What is your hobby: ")
    print type(user_hobby)
    print hobbies

oops, raw_input does not seem to be a string, but rather a unicode


can u explain further? When i did not put the str(), every item in the list will have a u something


That is because it is unicode (u of unicode):


gives as output:


Which is why you cast it to a string using str()


oh okay i see. Then how do u know when to use str()? sometimes printing list will print normally


Well, if it prints normally it is usually a string. The moment i write some code:

start = "hello world"
print start

i know start contains a string (since i wrote it), but the moment i fetch input (with for example raw_input, or from the web) i would check which data type i am dealing with (using type()).

You can just temporary add a type() function, to check what you are dealing with, and then remove it, since most likely this won't change. You now know that raw_input gives unicodes, so you now know (or write it down) that raw_input gives unicode


Thank You for your clear explaination! :slight_smile: