將字符串'321_1'
轉換爲'321.1'
。Python - 將下劃線轉換爲Fullstop
我想創建一個方法將下劃線轉換爲句號。我用拆分,但它不能工作..任何人都可以幫助我?或者我必須使用while循環
轉換下劃線句號
def Convert_toFullStop(text):
x1 = ""
words = text.split()
if words in ("_"):
words = "."
print words
將字符串'321_1'
轉換爲'321.1'
。Python - 將下劃線轉換爲Fullstop
我想創建一個方法將下劃線轉換爲句號。我用拆分,但它不能工作..任何人都可以幫助我?或者我必須使用while循環
轉換下劃線句號
def Convert_toFullStop(text):
x1 = ""
words = text.split()
if words in ("_"):
words = "."
print words
我會做
def Convert_toFullStop(text):
return text.replace('_', '.')
,離開print
給調用者。
使用replace()功能?
newtext = text.replace('_','.')
最好的是使用replace()
方法,因爲它在上面的答案中提出。
但是,如果你真的想用split()
:
words = text.split("_")
print ".".join(words)
通過用空格字符默認split()
方法拆分。
python就像口語。下次嘗試它之前,請問:-) – HongboZhu
@ user2837162不是一個函數,它是一個字符串對象的方法。 –
好吧謝謝:)它運作良好 – user2837162