2014-06-05 57 views
2

我目前正試圖在使用變量的三引號字符串內連接。什麼是去了解這一點的最好方法是什麼?三重引號字符串的連接

print(''' 
Points left to spend: ''' + str(pointsLeft) + ''' 
''' + str(attrChoice) + ':\t' + '''[''' + str(charAttr[attrChoice]) + '''] 
To reduce the number of points spent on this skill, simply enter a negative number. 
''' 
) 

我得到的錯誤信息是:關鍵字不能是表達式。任何人都可以解釋這意味着什麼,如果它可以嘗試這樣一個級聯?

+0

什麼'attrChoice','charAttr'等..? –

回答

10

要做到這一點,最好的辦法是str.format

template = """This is a 
multiline {0} with 
replacement {1} in.""" 

print(template.format("string", "fields")) 
+0

謝謝。這應該是有效的。我會標記你,但缺乏聲譽。 –

+0

@PedroRhian如果答案解決了您的問題,您可以接受它 - 請參閱http://stackoverflow.com/help/someone-answers – jonrsharpe

+0

如何通過同一字符串中的多個替換來實現這一點? –