2013-10-02 142 views
1

將字符串'321_1'轉換爲'321.1'Python - 將下劃線轉換爲Fullstop

我想創建一個方法將下劃線轉換爲句號。我用拆分,但它不能工作..任何人都可以幫助我?或者我必須使用while循環

轉換下劃線句號

def Convert_toFullStop(text): 

    x1 = "" 
    words = text.split() 
    if words in ("_"): 
     words = "." 
    print words 

回答

3

我會做

def Convert_toFullStop(text): 
    return text.replace('_', '.') 

,離開print給調用者。

7

使用replace()功能?

newtext = text.replace('_','.') 
+1

python就像口語。下次嘗試它之前,請問:-) – HongboZhu

+1

@ user2837162不是一個函數,它是一個字符串對象的方法。 –

+0

好吧謝謝:)它運作良好 – user2837162

1

最好的是使用replace()方法,因爲它在上面的答案中提出。

但是,如果你真的想用split()

words = text.split("_") 
print ".".join(words) 

通過用空格字符默認split()方法拆分。

相關問題