Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (19.9k points)

Hi. 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 it is not working.

1 Answer

0 votes
by (25.1k points)

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

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. 

You can do it like this:

name="abc000"

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

print(modified_name)

You can use this video to learn more about python strings and it's methods:

Interested to learn more about Python? Come & join our Python course

...