Dhruv Bhardwaj
Dhruv Bhardwaj

Reputation: 13

I don't know why this python code for replacing all occurences of a substring not working

#program to replace all occurences of a substring with a given substring!!!!

def replace_string(str,x,y):
    a = len(x)
    for i in range(len(str)):
        if str[i] == x[0]:   
            end = i+a
            sub_str = str[i:end]
            if x == sub_str:
                str = str[:i] + y + str[end:]
            else:
                continue
        else:
            continue
    print (str)

str = input("enter a string- ")
x = input("Input substring to be deleted- ")
y = input("Enter substring to be fit in- ")
replace_string(str,x,y)

These are some outputs it gives

1.

enter a string- abcabcddabc
Input substring to be deleted- abc
Enter substring to be fit in- haha

->hahahahaddhaha

2.

enter a string- i am a good boy
Input substring to be deleted-<space> 
Enter substring to be fit in- "

->i"am"a"good"boy

But when i put the same string "I am a good boy" but i try to replace good with bad, it gives an error of index out of range but i am not able to understand why this is coming....

enter a string- i am a good boy
Input substring to be deleted- good
Enter substring to be fit in- bad
Traceback (most recent call last):
  File "f:/----/-----/---------/Fn_replace.py", line 19, in <module>
    replace_string(str,x,y)
  File "f:/----/-----/--------/Fn_replace.py", line 5, in replace_string
    if str[i] == x[0]:
IndexError: string index out of range

PLEASE HELP

Upvotes: 1

Views: 70

Answers (2)

blackraven
blackraven

Reputation: 5597

This is because the new string bad is shorter than good. For debugging purpose, I add a padding character ^ to see the difference.

def replace_string(str,x,y):
    if len(y)<len(x):
        y += "^"*(len(x)-len(y))   #add padding
    a = len(x)
    for i in range(len(str)):
        if str[i] == x[0]:   
            end = i+a
            sub_str = str[i:end]
            if x == sub_str:
                str = str[:i] + y + str[end:]
            else:
                continue
        else:
            continue
    print (str)

Output:

enter a string-  i am a good boy
Input substring to be deleted-  good
Enter substring to be fit in-  bad
i am a bad^ boy

If you change the print statement to print(str.replace("^", "")), the new output would be

enter a string-  i am a good boy
Input substring to be deleted-  good
Enter substring to be fit in-  bad
i am a bad boy

For learning purpose, you could use the function .replace() to achieve your end goal

def replace_string(s,x,y):
    print(s.replace(x, y))

s = input("enter a string- ")
x = input("Input substring to be deleted- ")
y = input("Enter substring to be fit in- ")
replace_string(s,x,y)

Output

enter a string-  i am a good boy
Input substring to be deleted-  good
Enter substring to be fit in-  bad
i am a bad boy

P/S: Also, please follow @Sujay's advice: do not use 'str' as variable name, because it is a python function, example str(x)

Upvotes: 1

user15801675
user15801675

Reputation:

The length of bad isn't equal to good hence the error. Also, str is inbuilt function in python so don't use it as a variable. I suggest a simple program here.

strr = input("enter a string- ")
x = input("Input substring to be deleted- ")
y = input("Enter substring to be fit in- ")
strr=strr.replace(x,y)
print(strr)

Example:

strr=hello hello boys
x=hello
y=f

Output: f f hello

Upvotes: 1

Related Questions