Python string replace not working

1.6K    Asked by RutujaMishra in Python , Asked on Apr 5, 2021

As I am trying to replace some characters in python string but it is not working.

I am using this code:

name = abc000
name.replace(“000”,””)
It showed proper output in the python interpreter. Then I tried it in Pycharm by adding the following line
print(name)
but still not working. What to do if python replace not working.

Answered by Rutuja Mishra

You are getting this replacement issue because you are using the replace method incorrectly.

You can do it using the below code:

name="abc000"

modified_name=name.replace(“000”,””)

print(modified_name)

Note: When you call the replace method on a string in python you get a new string with the contents replaced as specified in the method call. You are not storing the modified string but are just using the unmodified string.



Your Answer

Interviews

Parent Categories