【Python面試題】書寫一個函數,用于替換某個字符串或幾個字符串
函數原型strreplace(str,oldString,newString)
例如:
s = 'Hello World!';
afterReplace = strreplace(s,'World','Tom')
輸出結果為:"Hello Tom!"
1
2
3
代碼實現:
def strreplace(str,oldString,newString):
str_list = str.split(oldString)
print(newString.join(str_list))
strrepalce('Hello World !','World','Tom')