(10/19) Why am I getting a [u, "user Input"] EXAMPLE : [u'buffet', u'sumo']


#1

hobbies = []

Add your code below!

for i in range(3):

Input = raw_input("Input your hobby:")

hobbies.append(Input)

print hobbies

#2

Don't do things like that...

Do it like this,

for i in range(3):
    hobbies.append(raw_input("Input your hobby:"))
print(hobbies)

#3

Your code is better, but I am genuinely curious on why it added that 'u'.


#4

It's a regular expression, that is a little too advanced for this course nor will they ever touch upon all the different things you can do with regular expressions in these course here.

Note: Regular Expressions are technically a mini-language.

EXAMPLE: This finds email@interweb.com addresses

import re

pattern = r"([\w\.-]+)@([\w\.-]+)(\.[\w\.]+)"
str = "Please contact info@here.com for assistance"

match = re.search(pattern, str)
if match:
   print(match.group())

#5

Bit of an omega for me (over my head) but I still appreciate the explanation and help you have given. Thanks for your 'Input' :smiley:
Ill mark this as solved for now.


#6

Hi @sonbot,

The 'u' in front of a string generally means that the string is in the unicode format.

As @zeziba said, it is beyond the introductory level that is taught here on Codecademy so I wouldn't worry myself with that for now :wink:

I just wanted to post the related Python Doc here just in case someone ends up looking up this thread for answers while searching the forum... :smile:


#7

use str() like this:
hobbies.append(str(raw_input("write a hobby")))


#8

I used a str as suggested and still could not enter the second hobby and still got the u before each of the hobbies - the second the same as the first!


#9

Thank you for the suggestion to cut down the code!


#10

Did you find the solution for this?


#11

Compare the following two approaches for displaying the output. The second one uses a list comprehension to convert each of the items in hobbies to a str, which omits the u.

# display the hobbies list as is
print hobbies

# display each item in the hobbies list as a str
print [str(hobby) for hobby in hobbies]

Output …

[u'hiking', u'photography', u'programming']
['hiking', 'photography', 'programming']

List comprehensions are introduced later on in the Learn Python track in Advanced Topics in Python: Building Lists.


#12

Hi,

Sorry I stopped using CodeAcademy…


#13

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