2014-05-16 2881 views
0

更具体地说,我试图以类似print str(function(first)) + str(function(second)) + str(function(third))等这样的句子的形式打印几个函数的结果,但是我遇到了这样的问题:单个字符串之间没有空格。Python:如何在字符串之间添加空格

所以,我想知道的是我如何在每个字符串之间添加空格。

回答

4

使用join()

print " ".join([str(function(first)), str(function(second)), str(function(third))]) 
+0

您也可以通过'join'发电机的表达,而不是一个名单,如果这是更方便: ''“.join(str(function(x))for [in first,second,third])' – Blckknght

+1

''”.join(map(lambda x:str(function(x) ),(第一,第二,第三)))' –

+0

由于我刚接触'join()',与仅执行print函数(第一个),函数(第二个)'相比,它有什么优点/等等。 – 5donuts

1
print str(function(first)) + ' ' + str(function(second)) + ' ' + str(function(third)) 
2
print function(first), function(second), function(third) 

或使用字符串格式化:

print '{0} {1} {2}'.format(function(first), function(second), function(third)) 
相关问题