0
我正在研究這個python函數,它應該有三個參數:第一個稱爲內容字符串,可以是任何字符串。第二個稱爲令牌的字符串應該是一個包含要在內容字符串中替換的單個字符的字符串。第三,應表示將在結果字符串中的新字符。一個函數,執行類似於python中的string.replace()方法的任務
我有這個至今:
def myReplace(aString, token, newToken):
cnt = 0
while(cnt < len(aString)):
if(aString[cnt] == token):
aString[cnt] == newToken
return aString[cnt]
cnt+=1
#print("Couldn't find "+token+" in "+aString)
return "can not find the token you are looking for in the String"
功能是假設與新的字符替換令牌字母(或多個)返回一個新的字符串,
>>> result = myReplace("Hello world!",'l','q')
>>> print(result)
Heqqo Worqd!
但它只返回一個字符。
>>> myReplace("hello world", 'l', 'g')
'h'
我試着刪除'cnt'變量,但這沒有幫助。
注:我不能使用內置的replace()方法來解決這個問題。
任何幫助將不勝感激,它只是我試圖在它上面,似乎沒有任何工作了。