My function has three string parameters i.e. string, search, and replace. If the search parameter is an empty string, then the function is supposed to insert the replaceable parameter before the first parameter, in between each character of the old string, and after the last character of the old string. Here is what I have done so far:
def main():
s='I am going go'
x=""
y='aa'
print(rep_str(s,x,y))
def rep_str(s, x, y):
if x in s:
result = ''
i = 0
while i < len(s):
if s[i : i + len(x)] == x:
result += y
i += len(x)
else:
result += s[i]
i += 1
elif x not in s:
result= s
else:
if x=="":
result=y
for ch in s:
result+=(ch+y)
return result
main()
I developed each condition separately and put them altogether in the function when I got satisfactory result from them. My last else condition was working fine in a separate run but it is not working in the function module. I don't know what is the problem with the code. I would appreciate, if someone could give me some pointers. Thanks
My output for the last else condition should be:
aaIaa aaaaamaa aagaaoaaiaanaagaa aagaaoaa
xis eitherin sornot in s, whatelsedo you think could happen? :)'' in 'cobra'→ Truereplacemethod already does this. Can you just use that?