For your hobbies - "u" before list items


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


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


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


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: ")
print hobbies

other = []

for item in hobbies:
  print item
print other
Enter a hobby: programming
Enter a hobby: hiking
Enter a hobby: music
[u'programming', u'hiking', u'music']
['programming', 'hiking', 'music']


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


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

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


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


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.


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