Reputation: 315
How can I remove a string that contains apostrophes e.g.: I want to remove ' Cannot get 'data' from cell'
from my text.
i would use str.replace('Cannot get 'data' from cell','')
, but the apostrophes are "splitting" the string and so this doesnt work.
Upvotes: 0
Views: 157
Reputation: 681
I don't know if the other answers here answered your question, but it's confusing me. In what way "remove"? If you really want to remove it:
foo = 'Hello, World! This string has an \' in it!'
if "'" in foo: # if '\'' in foo is also possible
del foo
If you mean to replace the apostrophes with something try:
foo = 'Hello, World! This string has an \' in it!'
foo = foo.replace('\'', parameter2) #parameter2 is the value which you wanna replace the apostrophe with
Please be more specific with your questions in the future!
Upvotes: 0
Reputation: 2647
Just use double quotes to mark the string you want to remove, or use backslashes, though is more unclear.
string.replace("'Cannot get 'data' from cell'",'')
string.replace('\'Cannot get \'data\' from cell\'', '')
EDIT: If you don't have quotes before Cannot
and after cell
, you just need to remove first and last single quote from the string to be replaced
string.replace("Cannot get 'data' from cell",'')
string.replace('Cannot get \'data\' from cell', '')
Upvotes: 1
Reputation: 1106
You can escape single quotes using the backslash like this:
str.replace('Cannot get \'data\' from cell', '')
If you want to remove both the initial quotes and the ones in the middle, you should escape the first and last too like this:
str.replace('\'Cannot get \'data\' from cell\'', '')
Upvotes: 1