-
-
Save Nihhaar/703da5e4e2f5b8041576bfa1e3378e72 to your computer and use it in GitHub Desktop.
Python: Remove punctuation from string
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #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