Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

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.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 68535
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T19:25:54+00:00 2026-05-10T19:25:54+00:00

It seems like there should be a simpler way than: import string s =

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-10T19:25:55+00:00Added an answer on May 10, 2026 at 7:25 pm

    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 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Seems like there should be... Right now it just seems like magic that you
It seems like there should be a removalAllOccuring(Collection) (or similiar) method in Multiset. A
It seems like there used to be way more binary protocols because of the
This seems like it's far more difficult than it should be, but I'd like
I am used to the c-style getchar() , but it seems like there is
I been wondering about this for a while. It seems like there are so
It seems to me like there's a lot of sheeping going on, with everyone
cgi.escape seems like one possible choice. Does it work well? Is there something that
Well is there? From everything I've read, it seems like the answer is no,but
Is there any intrinsic support for namespacing in coffeescript? Adequate namespacing seems like something

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.