Best way to strip punctuation from a string
From an efficiency perspective, you're not going to beat
s.translate(None, string.punctuation)
For higher versions of Python use the following code:
s.translate(str.maketrans('', '', string.punctuation))
It's performing raw string operations in C with a lookup table - there's not much that will beat that but writing your own C code.
If speed isn't a worry, another option though is:
exclude = set(string.punctuation)
s = ''.join(ch for ch in s if ch not in exclude)
This is faster than s.replace with each char, but won't perform as well as non-pure python approaches such as regexes or string.translate, as you can see from the below timings. For this type of problem, doing it at as low a level as possible pays off.
Timing code:
import re, string, timeit
s = "string. With. Punctuation"
exclude = set(string.punctuation)
table = string.maketrans("","")
regex = re.compile('[%s]' % re.escape(string.punctuation))
def test_set(s):
return ''.join(ch for ch in s if ch not in exclude)
def test_re(s): # From Vinko's solution, with fix.
return regex.sub('', s)
def test_trans(s):
return s.translate(table, string.punctuation)
def test_repl(s): # From S.Lott's solution
for c in string.punctuation:
s=s.replace(c,"")
return s
print "sets :",timeit.Timer('f(s)', 'from __main__ import s,test_set as f').timeit(1000000)
print "regex :",timeit.Timer('f(s)', 'from __main__ import s,test_re as f').timeit(1000000)
print "translate :",timeit.Timer('f(s)', 'from __main__ import s,test_trans as f').timeit(1000000)
print "replace :",timeit.Timer('f(s)', 'from __main__ import s,test_repl as f').timeit(1000000)
This gives the following results:
sets : 19.8566138744
regex : 6.86155414581
translate : 2.12455511093
replace : 28.4436721802
How to remove all the punctuation in a string? (Python)
A really simple implementation is:
out = "".join(c for c in asking if c not in ('!','.',':'))
and keep adding any other types of punctuation.
A more efficient way would be
import string
stringIn = "string.with.punctuation!"
out = stringIn.translate(stringIn.maketrans("",""), string.punctuation)
Edit: There is some more discussion on efficiency and other implementations here:
Best way to strip punctuation from a string in Python
How to strip string from punctuation except apostrophes for NLP
>>> from string import punctuation
>>> type(punctuation)
<class 'str'>
>>> my_punctuation = punctuation.replace("'", "")
>>> my_punctuation
'!"#$%&()*+,-./:;<=>?@[\\]^_`{|}~'
>>> "It's right, isn't it?".translate(str.maketrans("", "", my_punctuation))
"It's right isn't it"
Is there a way to remove punctuation at the start and end of each word in string but not the middle? Python
This should do it for you.
from string import punctuation
a="""'?hello !mango! and, ban,ana yum apple!', '?!,' """
new=[i.strip(punctuation) for i in a.split()]
print(" ".join(new))
output:
hello mango and ban,ana yum apple
Related Topics
Importing Files from Different Folder
Listing Available Devices in Python-Opencv
How to Create a Density Plot in Matplotlib
Actual Meaning of 'Shell=True' in Subprocess
Why Does This Unboundlocalerror Occur (Closure)
How to Avoid Having Class Data Shared Among Instances
Strange Result When Removing Item from a List While Iterating Over It
List of Lists Changes Reflected Across Sublists Unexpectedly
How to Clone a List So That It Doesn't Change Unexpectedly After Assignment
How to Remove Items from a List While Iterating
How to Remove Duplicates from a List, While Preserving Order
Is There a Difference Between "==" and "Is"
What Does If _Name_ == "_Main_": Do
Why Is the Command Bound to a Button or Event Executed When Declared