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

  • Home
  • SEARCH
  • 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

Ask A Question

Stats

  • Questions 227k
  • Answers 227k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I did it for you but don't know how to… May 13, 2026 at 1:19 am
  • Editorial Team
    Editorial Team added an answer You need to remove the num parameter from the figure… May 13, 2026 at 1:19 am
  • Editorial Team
    Editorial Team added an answer You could capture the WM_KEYDOWN message and ignore it if… May 13, 2026 at 1:19 am

Related Questions

I have a project where a function receives four 8-bit characters and needs to
I have a launchd daemon that every so often uploads some data via a
I've got a collection of strings, and I need to know the first index
I recently began using BIRT and have developed a report to use with my

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.