To remove all punctuation from a string in Python, the easiest way is with the Python string translate() function.
import string
s = "this#&*! is a *(*#string &!#@...'';[with punctuation!|}{"
print(s.translate(str.maketrans("","",string.punctuation)))
#Output:
this is a string with punctuation
Another way to remove all punctuation from a string is with a loop and the string replace() function.
import string
s = "this#&*! is a *(*#string &!#@...'';[with punctuation!|}{"
for char in string.punctuation:
a = a.replace(char,"")
#Output:
this is a string with punctuation
One last way to remove all punctuation from a string is with regex and the Python re module.
import re
s = "this#&*! is a *(*#string &!#@...'';[with punctuation!|}{"
print(re.sub('[^ws]', '', s))
#Output:
this is a string with punctuation
When using string variables in Python, the ability to easily be able to modify and change the value of these variables is important.
One such case is if you want to remove specific characters from a string.
To remove all punctuation from a string in Python, the easiest way is with the Python string translate() function.
translate() returns a copy of the string in which all characters have been translated using a transition table constructed with the maketrans() function in the string module.
You can also add characters to delete in the transition table and this is where we will use translate() to remove all punctuation from a string.
Below is a simple example of how you can use translate() to remove all punctuation from a string in Python.
import string
s = "this#&*! is a *(*#string &!#@...'';[with punctuation!|}{"
print(s.translate(str.maketrans("","",string.punctuation)))
#Output:
this is a string with punctuation
Using for Loop and replace() to Remove All Punctuation from String in Python
Another way you can remove all punctuation from a string is with a loop and the Python string replace() function
replace() takes a string to search for and a replacement value and replacing all found strings with the replacement value.
To remove all punctuation from a string in Python, you can search for specific punctuation and replace them with an empty string.
Therefore, we can loop over all the punctuation characters and replace them with empty strings to get rid of the punctuation in a string.
Below shows you how to use replace() to remove all punctuation in a string in Python.
import string
s = "this#&*! is a *(*#string &!#@...'';[with punctuation!|}{"
for char in string.punctuation:
a = a.replace(char,"")
#Output:
this is a string with punctuation
Using regex to Remove All Punctuation from String in Python
One other way you can remove all punctuation from a string is with regex. To perform regex substitution, we can use the Python re module sub() function.
We can easily define a regular expression which will search for punctuation, and then using the sub() function, we will replace the punctuation with an empty string.
Below are some examples of how you can remove all punctuation from strings using Python with the sub() function.
import re
s = "this#&*! is a *(*#string &!#@...'';[with punctuation!|}{"
print(re.sub('[^ws]', '', s))
#Output:
this is a string with punctuation
Hopefully this article has been useful for you to learn how to remove all punctuation from a string in Python.