Trying to accomplish exactly what my doctsring says but I'm having one issue. In one of my outputs, I can't figure out what's going wrong.
#Replace String
def replace_str(op1,op2,op3):
"""Returns the string which is a copy of the first parameter, but where \
all occurrences of the second parameter have been replaced by the third\
parameter"""
finalword=""
if op2 not in op1:
return op1
if op2 == "":
finalword+=op3
for i in op1:
finalword+=i+op3
return finalword
count=0
for i, ch in enumerate(op1):
count+=1
sliceword=op1[i:i+len(op2)]
if sliceword == op2:
count = len(op2)
finalword+=op3
else:
finalword+=ch
count-=1
return final word
Outputs:
g=replace_str("Hello World","o","test")
print("Hello World, o, test, returns:",g)
Hello World, o, test, returns: Helltest Wtestrld
g1=replace_str("Hello","o"," is not fun")
print("Hello, o, is not fun, returns:",g1)
Hello, o, is not fun, returns: Hell is not fun
g5=replace_str("HelloWorld","World","12345")
print("HelloWorld, World, 12345, returns:",g5)
HelloWorld, World, 12345, returns: Hello12345orld
A you can see for HelloWorld, World, 12345, I get Hello12345orld. My desired output is Hello12345
str.replaceif I understand you correctly.