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

时间:2013-01-07 16:29:02

标签: python python-2.7 python-2.x

我正在使用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)

输出变为 -

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.

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

1 个答案:

答案 0 :(得分:8)

不要使用%r。变化:

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

要:

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

repr()str()之间的区别在于repr()是文字的,并使用字符串打印出引号。

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

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