1

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

4
  • 3
    Are you doing this as an exercise? Because you seem to be duplicating the built-in str.replace if I understand you correctly. Commented Mar 2, 2015 at 22:48
  • 1
    your third ouput does not match to your output result. Commented Mar 2, 2015 at 22:49
  • Thanks for catching that, I just changed it @levi Commented Mar 2, 2015 at 22:51
  • 1
    yea i'm trying to recreate it to see how it works @Two-BitAlchemist Commented Mar 2, 2015 at 22:53

2 Answers 2

3

You are not advancing correctly in the input string in case of a match, notice how I changed the for loop with a while:

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
    i = 0
    while i < len(op1):
        sliceword=op1[i:i+len(op2)]
        if sliceword == op2:

            finalword+=op3
            i += len(op2)
        else:
            finalword+=op1[i]
            i += 1

    return finalword

g=replace_str("Hello World","World","test")
print("Hello World, o, test, returns:",g)
Sign up to request clarification or add additional context in comments.

Comments

2

you can use str.replace:

>>> "Hello World".replace("o","test")
'Helltest Wtestrld'

str.replace(old, new[, count])

Return a copy of the string with all occurrences of substring old replaced by new. If the optional argument count is given, only the first count occurrences are replaced.

if you want to recreate one, try pythonic:

>>> def str_replace(my_string, old, new):
...     return "".join(new if x == old else x for x in my_string)
...
>>> str_replace("Hello World", 'o', 'test')
'Helltest Wtestrld'

In the above code you can use str.lower to deal with case-sensitive

4 Comments

OP is not asking for a built-in function. He is trying to recreate it.
@levi the exercise shows that OP need to lean about str.replace. the formate looks like that
If OP want to recreate it to learn, that my friend is never a waste of time.
This will only work work if the string to substitute is a single char.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.