user2441441 user2441441 - 1 year ago 68
Python Question

Python remove all apostrophes from a string

I wanted to remove all occurrences of single and double apostrophes in lots of strings.

I tried this-

mystring = "this string shouldn't have any apostrophe - \' or \" at all"

It doesn't work though! Am I missing something?

Answer Source

Replace is not an in-place method, meaning it returns a value that you must reassign.

mystring = mystring.replace("'", "")
mystring = mystring.replace('"', "")

In addition, you can avoid escape sequences by using single and double quotes like so.