如何在字符串中打印函数?

时间:2014-05-29 23:27:00

标签: python string function

所以我尝试使用Python 2.7进行简单的计算,代码如下:

print 'In SchmooLand everyone is 9.33356564 times heavier than on earth.\n'

weight = float(raw_input('How much do you weigh on Earth in pounds?\n'))

print 'In SchmooLand you would weigh' 
print weight * 9.3356564 
print 'in pounds'

输出看起来像这样

How much do you weigh on Earth in pounds?
235423423
In SchmooLand you would weigh
2197832185.64
in pounds

如何在与字符串相同的行上打印重量计算的结果,以便它们像这样一起打印

In SchmooLand you would weigh 2197832185.64 in pounds

我总共n00b - 感谢您的帮助!

3 个答案:

答案 0 :(得分:4)

在每个打印行的末尾添加逗号以避免隐式换行:

print 'In SchmooLand you would weigh',
print weight * 9.3356564,
print 'in pounds',

来自print documentation

  

最后写一个'\ n'字符,除非print语句以逗号结尾。

答案 1 :(得分:1)

print 'In SchmooLand you would weigh {} in pounds'.format(weight * 9.3356564)

{}将被format()方法的参数替换。

答案 2 :(得分:1)

weight = float(raw_input('How much do you weigh on Earth in pounds?\n')) * 9.3356564

print 'In SchmooLand you would weigh', weight, 'in pounds'