The issue is with your string interpolation.
In your code, you use something like:
print "Your name is %r" % name
Instead, you either want to use:
print "Your name is %s" % name
...which makes Python manually treat name
as a string, or use:
print "Your name is {0}".format(name)
...which is the newer, more preferred way, and is less finicky to use.
Here's a breakdown of what's happening. When you use raw_input()
, Python is returning a special kind of string called a unicode string. Unicode strings are special in that they can represent all kinds of characters that a normal string can't, such as Chinese characters. Normal strings can generally use only the characters you see on your keyboard.
Now, in Python 2.x, you can indicate that a string is unicode by doing something like:
my_str = u"汉字/漢字"
Notice that the string is prefixed with a "u".
When you use the %r
interpolation indicator, you are telling Python to take your string, use repr
on the variable, and substitute it into the original string. If you do repr(my_str)
, it'll return u"汉字/漢字"
.
In contrast, if you use %s
, then Python will use str
on the variable. If you do str(my_str)
, it will return "汉字/漢字"
(sort of).
Unicode can be a tricky thing to understand, especially in Python. If you're interested, this presentation will go far more into depth on exactly what unicode is, and how it's used in Python.