Skip to content

Instantly share code, notes, and snippets.

@kissgyorgy
Created June 8, 2013 08:01
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save kissgyorgy/5734469 to your computer and use it in GitHub Desktop.
Save kissgyorgy/5734469 to your computer and use it in GitHub Desktop.
Python: Remove punctuation from string
#http://stackoverflow.com/questions/265960/best-way-to-strip-punctuation-from-a-string-in-python
import re, string
table = string.maketrans("","")
regex = re.compile('[%s]' % re.escape(string.punctuation))
def test_re(s): # From Vinko's solution, with fix.
return regex.sub('', s)
def test_trans(s):
return s.translate(table, string.punctuation)
#sets : 19.8566138744
#regex : 6.86155414581
#translate : 2.12455511093
#replace : 28.4436721802
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment