Domanda

I'm currently learning Python, and wrote a program to experiment with the language. However, whenever I use it, the output always has a letter "u" in it somewhere. I'm using Pyscripter as my IDE.

This is my code:

print "whats your name"
age = raw_input()
print "Alright, so %r, I just realized what percent-r does actually or is meant for" % (age)
print "What next ur age",
age1 = raw_input()
print "you entered %r " % (age1)

When I run it, I see something like this:

>>> Python 2.7.5 (default, May 15 2013, 22:43:36) [MSC v.1500 32 bit (Intel)] on win32.

>>> whats your name (i typed kk)

>>> Alright, so u'kk', i just realized what percent-r does actually or is meant for

>>> what next ur age (i typed ll)

>>> you entered u'll' 

Why is there a random u character inside my output, instead of just the string I want?

È stato utile?

Soluzione

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.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top