I need to strip the character "'" from a string in python. how do I do this? I know there is a simple answer. Really what I am looking for is how to write ' in my code. for example \n = newline.
+6
A:
As for how to represent a single apostrophe as a string in Python, you can simply surround it with double quotes ("'"
) or you can escape it inside single quotes ('\''
).
To remove apostrophes from a string, a simple approach is to just replace the apostrophe character with an empty string:
>>> "didn't".replace("'", "")
'didnt'
Mark Rushakoff
2010-06-30 16:10:03
Thanks, exactly what I was looking for
Richard
2010-06-30 16:12:32
+1
A:
Do you mean like this?
>>> mystring = "This isn't the right place to have \"'\" (single quotes)"
>>> mystring
'This isn\'t the right place to have "\'" (single quotes)'
>>> newstring = mystring.replace("'", "")
>>> newstring
'This isnt the right place to have "" (single quotes)'
sberry2A
2010-06-30 16:10:49
A:
You can simply use \'
within a string.
So, to strip out the character, you could do something along these lines:
mystring.replace('\'', '')
Alan Christopher Thomas
2010-06-30 16:11:46