For your hobbies - "u" before list items


#1

https://www.codecademy.com/courses/learn-python/lessons/loops/exercises/for-your-hobbies?action=lesson_resume

The code below is correct but I’m wondering why the console prints the list with the letter “u” before the list items:
[u’Coding’, u’Gaming’, u’Painting’, u’Cooking’]

hobbies = []

for hobby in range(4):
  hobby = raw_input("What is your hobby? ")
  hobby = hobbies.append(hobby)
  print hobbies

#2

Because the output going directly into the list from raw_input is Unicode.


#3

Oh, I see. So how would we be able to remove the “u” from the list?


#4

There are multiple ways, but this one is simplest…

hobbies = []

# Add your code below!
for n in range(3):
  hobby = raw_input("Enter a hobby: ")
  hobbies.append(hobby)
  
print hobbies

other = []

for item in hobbies:
  print item
  other.append(str(item))
  
print other
Enter a hobby: programming
Enter a hobby: hiking
Enter a hobby: music
[u'programming', u'hiking', u'music']
programming
hiking
music
['programming', 'hiking', 'music']

#5

Perfect! Thank you so much for your help! :slight_smile:


#6

You’re welcome. Here is an even simpler solution…

hobby = str(raw_input("Enter a hobby: "))

#7

This confuses me a bit because I thought that raw_input() already converted the input into a string.


#8

It is a string, but very likely UTF-16 since Python characters are 16 bit in order to accommadate international keyboards. The output of input in Python 3 is string, not unicode string, if i’m not mistaken, but I keep forgetting this stuff.


#11

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