2014-05-16 2877 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)) 
相關問題