不要在我的python代码中使用单引号

Aak*_*ash 0 python python-2.x python-2.7

我使用的是Python 2.7 ...我面临的问题是当我使用这段代码时

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, %r tall and %r heavy." % (
    age, height, weight)
Run Code Online (Sandbox Code Playgroud)

输出来了 -

How old are you? 35  
How tall are you? 6'2"  
How much do you weigh? 180lbs  
So, you're '35' old, '6\'2"' tall and '180lbs' heavy.
Run Code Online (Sandbox Code Playgroud)

但我不希望单引号出现在输出的第4行35左右,180磅6英寸2".怎么办

jac*_*ill 8

不要用%r.更改:

print "So, you're %r old, %r tall and %r heavy." % ( age, height, weight)
Run Code Online (Sandbox Code Playgroud)

至:

print "So, you're %s old, %s tall and %s heavy." % ( age, height, weight)
Run Code Online (Sandbox Code Playgroud)

之间的区别repr()str()repr()为文字,并打印出带有字符串引号.

这是解释器中的一个例子:

>>> print '%r' % 'Hi'
'Hi'
>>> print '%s' % 'Hi'
Hi
>>>