2

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?

Michael0x2a
  • 58,192
  • 30
  • 175
  • 224
Code Man
  • 105
  • 1
  • 2
  • 14

1 Answers1

6

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.

Michael0x2a
  • 58,192
  • 30
  • 175
  • 224
  • hey wow that was a quickest answer response to my query in life thanks a lot, so the issue is that %r will in general return a unicode prefix or a "u" prefixing my output sis i get it right also print "Your name is {0}".format(name) will take care of most/all other % formats (eg:- %s,%d,%r) for my input right – Code Man Sep 28 '13 at 07:20
  • 1
    @user2825656 -- I edited my answer with some more info. If you use `%r`, Python will substitute what the string looks like as literally as possible. If you use `%s`, then Python tries to use a more friendlier format. (In many cases, the two will function identically. It just so happens that wasn't the case here). – Michael0x2a Sep 28 '13 at 07:22
  • did i get it right print "Your name is {0}".format(name) will take care of most/all other % formats (eg:- %s,%d,%r) for my input right hey really thanx a lot for ur help, i am nearing my clarifications very fast just a couple more and i am through – Code Man Sep 28 '13 at 07:31
  • 1
    @user2825656 -- That's correct. The newer style (added in Python 2.6 and higher) is [preferred](http://stackoverflow.com/q/5082452/646543) over the older version. – Michael0x2a Sep 28 '13 at 07:35
  • ok thank you so very much man, i am just going to try it("Your name is {0}".format(name)) and will feedback in a few mins, thnx a lot (ps: unicode i just overall looked the presentation of unicode is not really that tricky to understand it just needs more time to read and a bit patience both of which m not good at lolz) – Code Man Sep 28 '13 at 07:43
  • @user2825656 -- No problem. If you determine that your problem is solved, click the checkmark to the upper-left of my answer to mark your question as solved. – Michael0x2a Sep 28 '13 at 07:45
  • HEEYYYY!!!! ALLLRRRIIIIIGHT!!!!! IT WORKED.......and as opposed to the other people's comments on stackoverflow saying literally that u cant "get rid of it" and "face it" and "suck it up" ........I ACTUALLY GOT RID OF IT. THNX.......JUST ONE LAST QUERY this(print "Your name is {0}".format(name)) takes care of all the formats i believe, coz i tried it with digits specialchars and strings ............its taking anythng i write,,,, so all formats accepted by doing this am i right???...or am i wrong...... – Code Man Sep 28 '13 at 07:59
  • @user2825656 -- basically, yep. That's why `format` is preferred -- things tend to just *work* by default. Here are some [more examples](http://docs.python.org/2/library/string.html#formatexamples) of how to use it. (Also, technically, you're not "getting rid of it" -- it's still there, but you're just hiding it from view :) (sort of). Cheers! – Michael0x2a Sep 28 '13 at 08:05
  • no no no i get it, i know i am not getting rid of it actually its just that i am getting rid of its public display lolz...........i know others are right but they are wrong when the question was about how to stop the display of it and not actually getting rid of it and these "no u cant" kinds of answers were wrongly said on that question........thanx man u helped me through this faster than i thot.......my questions answered sucessfully thnx. Cheers – Code Man Sep 28 '13 at 08:11