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.
Bhargav Rao
47.3k27 gold badges121 silver badges137 bronze badges
asked Jun 30, 2010 at 16:07
1
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'
answered Jun 30, 2010 at 16:10
Mark RushakoffMark Rushakoff
241k44 gold badges401 silver badges395 bronze badges
0
Here are a few ways of removing a single '
from a string in python.
str.replace
replace
is usually used to return a string with all the instances of the substring replaced."A single ' char".replace["'",""]
str.translate
In Python 2
To remove characters you can pass the first argument to the funstion with all the substrings to be removed as second.
"A single ' char".translate[None,"'"]
In Python 3
You will have to use
str.maketrans
"A single ' char".translate[str.maketrans[{"'":None}]]
re.sub
Regular Expressions using
re
are even more powerful [but slow] and can be used to replace characters that match a particular regex rather than a substring.re.sub["'","","A single ' char"]
Other Ways
There are a few other ways that can be used
but are not at all recommended. [Just to learn new ways]. Here we have the given string as a variable string
.
Using list comprehension
''.join[[c for c in string if c != "'"]]
Using generator Expression
''.join[c for c in string if c != "'"]
Another final method can be used also [Again not recommended - works only if there is only one occurrence ]
Using
list
call along withremove
andjoin
.x = list[string] x.remove["'"] ''.join[x]
answered Jul 17, 2015 at 18:29
Bhargav RaoBhargav Rao
47.3k27 gold badges121 silver badges137 bronze badges
1
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]'
answered Jun 30, 2010 at 16:10
sberrysberry
124k18 gold badges135 silver badges165 bronze badges
You can escape the apostrophe with a \
character as well:
mystring.replace['\'', '']
answered Jun 30, 2010 at 16:11
1
Python is the best programming language for making complex code into simpler code. And it’s due to High Level and Just like English language programming language. For example, you want to replace a single quote in the string. If you will use the other programming language you may find difficulty. But in the case of Python, it’s very easy. In this entire tutorial, you will know the various method to replace a single quote from the string using python.
Before going to the coding demonstration part you should make sure that you do all the coding parts on the Jupyter Notebook. As I am also doing on it, so it makes you able to understand more about this tutorial.
Method 1 : Using the replace[] method
Python provides an inbuilt function for a quicker solution to the problem. The replace[] method is on of them. To replace a single quote from the string you will pass the two parameters. The first is the string you want to replace and the other is the string you want to place. In our case it is string.replace[” ‘ “,” “].
Execute the below lines of code.
string = "Hello! Data Science' Learner"
string.replace["'",""]
Output
Method 2: Using maketrans[]/translate[] method
There are also other functions to replace a single quote from a string in python. It is the maketrans[] and translate[]. Inside the translate[] function you will pass the key-value pair with the {}. The key will be the string you want to replace and the value will be the string to place. After that, you will make its result as an argument for the maketrans[]function.
Run the below lines of code.
string = "Hello! Data Science' Learner"
string.translate[string.maketrans[{"'":None}]]
Output
Method 3: Replace a single quote in python using Regex
The third method to replace a single quote is the use of the regex module. Regex is one of the basic building blocks in Text Mining and Analytics. For this example, you have to use the sub[]function that accepts three parameters. One is the string you want to replace, the second is the string you want to place and the third is the input string.
Execute the below lines of code to replace a single quote in python.
import re
string = "Hello! Data Science' Learner"
re.sub["'","",string]
Output
Method 4: Using the list
You can also use the list to replace the single quote. You have to first convert the input string to a list and then remove the single quote from the list. At last, you have to join each list value to get the final string.
string = "Hello! Data Science' Learner"
s = list[string]
s.remove["'"]
''.join[s]
Output
Conclusion
Replacing some strings is a must if you pre-processing the datasets. However, in this tutorial, the implementation is for only understanding the concept. These are the methods to replace a single quote in a string using python. You can use these methods to replace anything from the string.
I hope you have liked this tutorial. If you are facing any difficulties then you can contact us for more help.
Join our list
Subscribe to our mailing list and get interesting stuff and updates to your email inbox.
We respect your privacy and take protecting it seriously
Thank you for signup. A Confirmation Email has been sent to your Email Address.
Something went wrong.