The task is to transform any string into any string without built-in .replace(). I failed because I forgot that technically space is also a string character. Firstly I transformed this string into the list, but now I see I did it unnecessarily. However, it still doesn't work.
- I can replace "cat" into "dog"
- I can replace "c" into "dog"
I can't replace "a cat" into "a dog".
I tried with lambda or zip, but I don't really know how to do it. Do you have any clue?
string = "Alice has a cat, a cat has Alice."
old = "a cat"
new = "a dog"
def rplstr(string,old,new):
""" docstring"""
result = ''
for i in string:
if i == old:
i = new
result += i
return result
print rplstr(string, old, new)
re.sub?return new...