我有這樣的代碼:字符串串聯在Python中產生不正確的輸出?
filenames=["file1","FILE2","file3","fiLe4"]
def alignfilenames():
#build a string that can be used to add labels to the R variables.
#format goal: suffixes=c(".fileA",".fileB")
filestring='suffixes=c(".'
for filename in filenames:
filestring=filestring+str(filename)+'",".'
print filestring[:-3]
#now delete the extra characters
filestring=filestring[-1:-4]
filestring=filestring+')'
print "New String"
print str(filestring)
alignfilenames()
我試圖讓字符串變量看起來像這樣的格式:suffixes=c(".fileA",".fileB".....)
,但增加在最後括號是行不通的。當我運行此代碼時,我得到:
suffixes=c(".file1",".FILE2",".file3",".fiLe4"
New String
)
任何想法發生了什麼或如何解決它?
對於'.join()'而不是'+ ='而言+1。投擲一個'.lower()',它看起來像他想要的。 – 2010-08-19 20:00:14
我認爲低層不是必需的,因爲他的例子沒有說明它的必要性。他的代碼不會嘗試小寫輸入,他所需的格式包括''.fileA''和''fileB'',但是他的輸入有'file1'和''file2'' – sberry 2010-08-19 20:02:05
是的!這是完美的,更簡潔!謝謝! – Brian 2010-08-19 20:06:02