Skip to main content
 首页 » 编程设计

python之如何在字符串中打印函数

2025年05月04日20over140

所以我尝试使用 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 - 感谢您的帮助!

请您参考如下方法:

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

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

来自print documentation :

A '\n' character is written at the end, unless the print statement ends with a comma.