不使用正则表达式实现Python的replace()函数

jwe*_*nga 3 python string function

我正在尝试在不使用正则表达式的情况下重写 python Replace() 函数的等效项。使用这段代码,我成功地使其能够处理单个字符,但不能处理多个字符:

def Replacer(self, find_char, replace_char):
    s = []
    for char in self.base_string:
        if char == find_char:
            char = replace_char
        #print char
        s.append(char)
    s = ''.join(s)

my_string.Replacer('a','E')
Run Code Online (Sandbox Code Playgroud)

有人能指点一下如何让这项工作与多个角色一起工作吗?例子:

my_string.Replacer('kl', 'lll') 
Run Code Online (Sandbox Code Playgroud)

Mik*_*eyB 5

你想变得多聪明?

def Replacer(self, find, replace):
    return(replace.join(self.split(find)))

>>> Replacer('adding to dingoes gives diamonds','di','omg')
'adomgng to omgngoes gives omgamonds'
Run Code Online (Sandbox Code Playgroud)