User input '1' appends [u'1'] to a list


#1

Hello

I have this piece of code

hobbies = []

# Add your code below!
n = ''

def st_nd_rd(number):
    if number == 1:
        return 'st'
    elif number == 2:
        return 'nd'
    elif number == 3:
        return 'rd'
    
st_nd_rd(1)

for x in range(1, 4):
    hobbies.append(raw_input("What is your %s\'%s hobby?" % (x, st_nd_rd(x))))
    print(hobbies)

Which just asks for input and appends it to a list

But I get this output

What is your 1'st hobby? 1
[u'1']
What is your 2'nd hobby? 2
[u'1', u'2']
What is your 3'rd hobby? 3
[u'1', u'2', u'3']
None

Where did the 'u' in the list come from? Is it intentional to indicate user input?


Why it prints 'u'
#2

the u is of uni-code, codecademy stores the input as unicode rather then string, to solve the problem, simply cast the result of raw_input to string


#4

honestly? I wouldn't bother, changes of encountering outside of codecademy is pretty slim

You can always google it, pretty sure there is a wikipedia page about it


#6

i have honestly no idea, i think i know someone who might now, let me get back to you once i found out


#7

Unicode is a good system, because it handles a wide range of characters from numerous languages. Python 2 and Python 3 both support it, however Python 3 support for Unicode is much more elegant than that of Python 2.

In Python 2, Unicode characters are supported by the unicode type. See Python 2.x’s Unicode Support.

Unicode support is much nicer in Python 3. In that version of Python, Unicode is very conveniently built into the str type. See Python’s Unicode Support.

The quality of Codecademy's support of Unicode seems to vary among the different Python exercises for some reason. For the For your hobbies exercise, you could do this to compensate for the display problem ...

for hobby in hobbies:
    print "{:s}".format(hobby)

With a little extra programming, there are additional means of handling Unicode in Python 2.

The following little program, executed in Python 3.6, demonstrates that Python 3 not only performs output of Unicode characters such as π quite smoothly, but it can even accommodate variable names that include such characters ...

import math
π = math.pi
print("π is approximately {:0.8f}".format(π))

Output ...

π is approximately 3.14159265

Hobbies print "u" infront of input
Weird append
#8

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