tags:

views:

88

answers:

3

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
Thanks, exactly what I was looking for
Richard
+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
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