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 7742047
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T09:11:02+00:00 2026-06-01T09:11:02+00:00

I want to remove characters in a string in python: string.replace(‘,’, ”).replace(!, ”).replace(:, ”).replace(;,

  • 0

I want to remove characters in a string in python:

string.replace(',', '').replace("!", '').replace(":", '').replace(";", '')...

But I have many characters I have to remove. I thought about a list

list = [',', '!', '.', ';'...]

But how can I use the list to replace the characters in the string?

  • 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. Editorial Team
    Editorial Team
    2026-06-01T09:11:04+00:00Added an answer on June 1, 2026 at 9:11 am

    If you’re using python2 and your inputs are strings (not unicodes), the absolutely best method is str.translate:

    >>> chars_to_remove = ['.', '!', '?']
    >>> subj = 'A.B!C?'
    >>> subj.translate(None, ''.join(chars_to_remove))
    'ABC'
    

    Otherwise, there are following options to consider:

    A. Iterate the subject char by char, omit unwanted characters and join the resulting list:

    >>> sc = set(chars_to_remove)
    >>> ''.join([c for c in subj if c not in sc])
    'ABC'
    

    (Note that the generator version ''.join(c for c ...) will be less efficient).

    B. Create a regular expression on the fly and re.sub with an empty string:

    >>> import re
    >>> rx = '[' + re.escape(''.join(chars_to_remove)) + ']'
    >>> re.sub(rx, '', subj)
    'ABC'
    

    (re.escape ensures that characters like ^ or ] won’t break the regular expression).

    C. Use the mapping variant of translate:

    >>> chars_to_remove = [u'δ', u'Γ', u'ж']
    >>> subj = u'AжBδCΓ'
    >>> dd = {ord(c):None for c in chars_to_remove}
    >>> subj.translate(dd)
    u'ABC'
    

    Full testing code and timings:

    #coding=utf8
    
    import re
    
    def remove_chars_iter(subj, chars):
        sc = set(chars)
        return ''.join([c for c in subj if c not in sc])
    
    def remove_chars_re(subj, chars):
        return re.sub('[' + re.escape(''.join(chars)) + ']', '', subj)
    
    def remove_chars_re_unicode(subj, chars):
        return re.sub(u'(?u)[' + re.escape(''.join(chars)) + ']', '', subj)
    
    def remove_chars_translate_bytes(subj, chars):
        return subj.translate(None, ''.join(chars))
    
    def remove_chars_translate_unicode(subj, chars):
        d = {ord(c):None for c in chars}
        return subj.translate(d)
    
    import timeit, sys
    
    def profile(f):
        assert f(subj, chars_to_remove) == test
        t = timeit.timeit(lambda: f(subj, chars_to_remove), number=1000)
        print ('{0:.3f} {1}'.format(t, f.__name__))
    
    print (sys.version)
    PYTHON2 = sys.version_info[0] == 2
    
    print ('\n"plain" string:\n')
    
    chars_to_remove = ['.', '!', '?']
    subj = 'A.B!C?' * 1000
    test = 'ABC' * 1000
    
    profile(remove_chars_iter)
    profile(remove_chars_re)
    
    if PYTHON2:
        profile(remove_chars_translate_bytes)
    else:
        profile(remove_chars_translate_unicode)
    
    print ('\nunicode string:\n')
    
    if PYTHON2:
        chars_to_remove = [u'δ', u'Γ', u'ж']
        subj = u'AжBδCΓ'
    else:
        chars_to_remove = ['δ', 'Γ', 'ж']
        subj = 'AжBδCΓ'
    
    subj = subj * 1000
    test = 'ABC' * 1000
    
    profile(remove_chars_iter)
    
    if PYTHON2:
        profile(remove_chars_re_unicode)
    else:
        profile(remove_chars_re)
    
    profile(remove_chars_translate_unicode)
    

    Results:

    2.7.5 (default, Mar  9 2014, 22:15:05) 
    [GCC 4.2.1 Compatible Apple LLVM 5.0 (clang-500.0.68)]
    
    "plain" string:
    
    0.637 remove_chars_iter
    0.649 remove_chars_re
    0.010 remove_chars_translate_bytes
    
    unicode string:
    
    0.866 remove_chars_iter
    0.680 remove_chars_re_unicode
    1.373 remove_chars_translate_unicode
    
    ---
    
    3.4.2 (v3.4.2:ab2c023a9432, Oct  5 2014, 20:42:22) 
    [GCC 4.2.1 (Apple Inc. build 5666) (dot 3)]
    
    "plain" string:
    
    0.512 remove_chars_iter
    0.574 remove_chars_re
    0.765 remove_chars_translate_unicode
    
    unicode string:
    
    0.817 remove_chars_iter
    0.686 remove_chars_re
    0.876 remove_chars_translate_unicode
    

    (As a side note, the figure for remove_chars_translate_bytes might give us a clue why the industry was reluctant to adopt Unicode for such a long time).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this string that has illegal chars that I want to remove but
I have a string and want to remove all non-symbolic characters (exclude ' ').
I have a list of legal characters and I want to remove all others
I want to remove all special characters from a string. Allowed characters are A-Z
I want to remove all non-alphanumeric and space characters from a string. So I
I want to remove the first characters from a string. Is there a function
I'm trying to remove the last 3 characters from a string in Python, I
I want to remove all strange characters from a string to make it url
I want to remove all special characters except space from a string using JavaScript.
I have a string in format /remove_this/I_want_this_onward/any_number_of_characters . I want to remove /remove_this and

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.