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?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
From an efficiency perspective, you’re not going to beat
For higher versions of Python use the following code:
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:
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:
This gives the following results: