Charlie
Charlie

Reputation: 19

Replace String Python

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

Upvotes: 1

Views: 313

Answers (2)

Hackaholic
Hackaholic

Reputation: 19771

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

Upvotes: 2

igon
igon

Reputation: 3066

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)

Upvotes: 3

Related Questions