2

I'm doing example 11 on Learn the Hard Way and I'm just curious of how to use an escape sequence for the height part. If I ask for how tall and someone types in 6'2" with the foot and inches as quotes how do I get something in there that will not show that backslash when running it? When you run it the raw input prints out like 6\'2". I've tried a few things and can't get nothing to work.

    print "How tall are you?",
    height = raw_input()
pynoob00
  • 29
  • 1
  • 4

3 Answers3

2

In this exercise you're asked to call the information you got from raw_input (the variable 'height' that now has a value '6'2"') and print it inside a sentence, by using %r.

There are different '%' types you can use, and %r will give you the kind of 'raw' representation of the string. Zed has asked you to figure out the difference between %r and %s and %d already, so you should be familiar with this. If not, read more here:

What's the difference between %r, %s and %d in python?

What does %s mean in Python?

To answer your question, this should work without problem:

print "How tall are you?"
height = raw_input()

print "You are %s tall" % height
Community
  • 1
  • 1
Marijke Vonk
  • 418
  • 3
  • 12
1

I think what you're observing is the representation of the string. Observe:

>>> height = raw_input()
6'2'' <-- my input
>>> height
"6'2''"
>>> print height
6'2''

When you just type height, it will display the representation of a string (i.e, if you were to assign that value to a string you'd get the same as the input). This can also be observed through the function repr():

>>> print repr(height)
"6'2''"

Or even!

>>> repr(height)
'"6\'2\'\'"'

So basically, when you don't call print, python is basically doing print repr(... for you.


More info at the python docs for the repr() function

TerryA
  • 58,805
  • 11
  • 114
  • 143
0

The answer of your question is:

print "How old are you?",
age = raw_input()
print "How tall are you?",
height = raw_input()
print "How much do you weigh?",
weight = raw_input()

print "So, you're %r old, %s tall and %r heavy." % (age, height, weight)

%r gives you raw representation of data where %s gives you to insert(and potentially format) a string.