Implement python replace() function without using regexp

Posted by jwesonga on Stack Overflow See other posts from Stack Overflow or by jwesonga
Published on 2011-11-15T16:33:31Z Indexed on 2011/11/15 17:51 UTC
Read the original article Hit count: 250

Filed under:
|
|
|

I'm trying to rewrite the equivalent of the python replace() function without using regexp. Using this code, i've managed to get it to work with single chars, but not with more than one character:

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')

Anybody have any pointers how to make this work with more than one character? example:

my_string.Replacer('kl', 'lll') 

© Stack Overflow or respective owner

Related posts about python

Related posts about string