Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best way to strip punctuation from a string

It seems like there should be a simpler way than:

import string s = "string. With. Punctuation?" # Sample string  out = s.translate(string.maketrans("",""), string.punctuation) 

Is there?

like image 733
Lawrence Johnston Avatar asked Nov 05 '08 17:11

Lawrence Johnston


People also ask

How do you remove punctuation from a string?

One of the easiest ways to remove punctuation from a string in Python is to use the str. translate() method. The translate method typically takes a translation table, which we'll do using the . maketrans() method.

How do you strip punctuation from a string in Python?

Use regex to Strip Punctuation From a String in Python The regex pattern [^\w\s] captures everything which is not a word or whitespace(i.e. the punctuations) and replaces it with an empty string.

How do I remove punctuation from a panda string?

To remove punctuation with Python Pandas, we can use the DataFrame's str. replace method. We call replace with a regex string that matches all punctuation characters and replace them with empty strings. replace returns a new DataFrame column and we assign that to df['text'] .


2 Answers

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 
like image 93
Brian Avatar answered Sep 28 '22 06:09

Brian


Regular expressions are simple enough, if you know them.

import re s = "string. With. Punctuation?" s = re.sub(r'[^\w\s]','',s) 
like image 38
Eratosthenes Avatar answered Sep 28 '22 06:09

Eratosthenes