Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python remove all apostrophes from a string

Tags:

python

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"
print(mystring)
mystring.replace("'","")
mystring.replace("\"","")
print(mystring)

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

like image 934
user2441441 Avatar asked Dec 04 '22 04:12

user2441441


1 Answers

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.

like image 78
Malik Brahimi Avatar answered Dec 08 '22 05:12

Malik Brahimi