在Python中,如何将raw_input的答案插入字符串的中间?

时间:2015-03-25 00:20:54

标签: python python-2.7

def number_of_legs():
    print " The sparrow has how many legs"
    legs = raw_input()
    print "You said the sparrow has ",+ legs"legs" 


number_of_legs()

4 个答案:

答案 0 :(得分:3)

你忘记了串联加号:

print "You said the sparrow has " + legs + " legs" 

尝试使用字符串格式:

print "You said the sparrow has {0} legs".format(legs) 

答案 1 :(得分:3)

使用str.format()

print " The sparrow has how many legs"
legs = raw_input()
print "You said the sparrow has {} legs".format(legs)

将输出:(如果腿是15)

You said the sparrow has 15 legs

另外,我建议你可以将一个字符串作为参数放到raw_input()。像这样:

legs = raw_input("The sparrow has how many legs? ")
print "You said the sparrow has {} legs".format(legs)

当我运行时:

The sparrow has how many legs? 25        # 25 is my input and that's it
You said the sparrow has 25 legs

答案 2 :(得分:1)

def number_of_legs():
    print " The sparrow has how many legs"
    legs = raw_input()
    print "You said the sparrow has, "+legs+"legs"
number_of_legs()

试试这个。只需要按顺序获取引号,加号和逗号。

答案 3 :(得分:1)

使用str.format

print "You said the sparrow has {0} legs".format(legs)