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).

Answer from georg on Stack Overflow
Top answer
1 of 16
285

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).

2 of 16
121

You can use str.translate():

s.translate(None, ",!.;")

Example:

>>> s = "asjo,fdjk;djaso,oio!kod.kjods;dkps"
>>> s.translate(None, ",!.;")
'asjofdjkdjasooiokodkjodsdkps'
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-replace-different-characters-in-string-at-once
Python - Replace Different Characters in String at Once - GeeksforGeeks
July 15, 2025 - List comprehension iterates over ... characters back into a single string. This method uses re.sub() function from Python's re module to replace characters in a string....
Discussions

Trying to replace characters in a list of strings?
Strings are immutable, replace doesn't operate in place but returns a new string. x = x.replace("cy","fi") Note that the whole thing can be done as a list comprehension: newlist = [ x.replace("cy","fi") for x in list] Also, don't call your own variables 'list'. More on reddit.com
🌐 r/learnpython
9
10
January 9, 2023
How to replace a specific index position in a list
Hello! So, just as the title says, how do I replace a specific index position? If you look at the bottom where it says blankword.replace(blankword[i],guess), that is what I’m having trouble with. I think I know why it doesn’t work, but I don’t know what else to do. More on discuss.python.org
🌐 discuss.python.org
4
0
November 3, 2021
Best way to replace multiple element from list in string?
You want to remove those characters? Easiest way I know of is to use the str.translate method. remove_punctuation = str.maketrans('','', "?:*<>|/\\") dirty_data = "hello: world?" clean_data = dirty_data.translate(remove_punctuation) print(clean_data) More on reddit.com
🌐 r/learnpython
4
1
February 8, 2021
Str.replace of a set of characters - Ideas - Discussions on Python.org
I would like str.replace, when given a set of characters, to replace occurrence of any of the characters in the set. I.e. 'ASDFGH'.replace(set('SFH'), '') == 'ADG' should then hold. It would mean I would not have to go to the trouble of using the re module for a common string operation. More on discuss.python.org
🌐 discuss.python.org
0
December 1, 2019
🌐
Python.org
discuss.python.org › ideas
Making str.replace() accept lists - Ideas - Discussions on Python.org
May 9, 2020 - Syntax of the method: str.replace(old, new, count=-1) What if replace could accept two lists instead of two strings. Often I find in code: text.replace(a, b).replace(c, d) The concatenation of replace calls can cause…
🌐
StrataScratch
stratascratch.com › blog › how-to-replace-a-character-in-a-python-string
How to Replace a Character in a Python String - StrataScratch
October 18, 2024 - Actually, it isn’t because it’s one of the three steps required in total: ... To convert a string to a list, use the split() method with this generic syntax. string_variable = 'your string here' list_of_words = string_variable.split() Now, ...
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-replace-substring-in-list-of-strings
Replace substring in list of strings - Python - GeeksforGeeks
July 11, 2025 - The replace() function replaces all occurrences of "world" with "universe" in each string. List comprehension iterates through the list and applies replace() to every string.
🌐
FavTutor
favtutor.com › blogs › replace-character-string-python
Python Replace Character in String | FavTutor
October 6, 2021 - Below are 6 common methods used to replace the character in strings while programming in python. Slicing is a method in python which allows you to access different parts of sequence data types like strings, lists, and tuples. Using slicing, you can return a range of characters by specifying the start index and end index separated by a colon and return the part of the string.
Find elsewhere
🌐
Real Python
realpython.com › replace-string-python
How to Replace a String in Python – Real Python
October 22, 2025 - In this tutorial, you'll learn how to remove or replace a string or substring. You'll go from the basic string method .replace() all the way up to a multi-layer regex pattern using the sub() function from Python's re module.
🌐
W3Schools
w3schools.com › python › ref_string_replace.asp
Python String replace() Method
Remove List Duplicates Reverse ... Study Plan Python Interview Q&A Python Training ... The replace() method replaces a specified phrase with another specified phrase. Note: All occurrences of ......
🌐
Mimo
mimo.org › glossary › python › string-replace-method
Master Python's String Replace Method for Text Manipulation
The syntax of this method ensures clarity and flexibility, allowing developers to easily modify strings without altering the original data. In the Python programming languages, replace() is useful for replacing characters and other substrings within strings.
🌐
Finxter
blog.finxter.com › home › learn python blog › 5 best ways to replace list of characters in a string with python
5 Best Ways to Replace List of Characters in a String with Python - Be on the Right Side of Change
February 20, 2024 - The str.replace() method in Python replaces occurrences of a substring within a string. By iterating over the list of characters, str.replace() can be used to systematically replace each character with another.
🌐
TutorialsPoint
tutorialspoint.com › python-replace-multiple-characters-at-once
Python - Replace multiple characters at once
October 3, 2023 - This is the most straightforward method using Python's built-in replace function to replace desired characters ? string = "Total Tap if Tol on Treep is obvious" replace_dict = {"T": "S", "o": "w"} for old, new in replace_dict.items(): string = string.replace(old, new) print(string)
🌐
Python.org
discuss.python.org › python help
How to replace a specific index position in a list - Python Help - Discussions on Python.org
November 3, 2021 - Hello! So, just as the title says, how do I replace a specific index position? If you look at the bottom where it says blankword.replace(blankword[i],guess), that is what I’m having trouble with. I think I know why it do…
🌐
DigitalOcean
digitalocean.com › community › tutorials › python-remove-character-from-string
How to Remove Characters from a String in Python | DigitalOcean
Remove characters from a Python string with replace(), translate(), re.sub(), and slicing. Compare methods, see examples, and pick the right approach.
🌐
Reddit
reddit.com › r/learnpython › best way to replace multiple element from list in string?
r/learnpython on Reddit: Best way to replace multiple element from list in string?
February 8, 2021 -

let's say I have string,

hello: world?

and I have list of characters

nameforbid = ["?", ":", "*", "<", ">", "|", "/", '\\']

Is there any way to replace every one of characters from list in string?

so I get new string

hello world

I googled few times and I got

any(forb in name for forb in nameforbid)

this checks whether the string has any element from the list.

This returns bool, not a string or character. Is there anyway to improve this?

🌐
iO Flood
ioflood.com › blog › using-python-to-replace-characters-in-a-string
Using Python to Replace Characters in a String
November 23, 2023 - You can also replace a sequence of characters. For instance, if you have the string ‘Thumbs up, thumbs down’ and you wish to replace ‘thumbs’ with ‘hands’, you would do it like this: message = 'Thumbs up, thumbs down' new_message = message.replace('thumbs', 'hands') print(new_message) # Outputs: Hands up, hands down · Python provides a wealth of built-in methods for string manipulation, making it a powerful language for handling text data.
🌐
TutorialsPoint
tutorialspoint.com › python-program-to-replace-a-character-at-a-specific-index
Python program to change character of a string using given index
July 11, 2023 - def change_multiple_characters(s, changes): char_list = list(s) for index, char in changes.items(): if 0 <= index < len(char_list): char_list[index] = char return ''.join(char_list) # Example usage s = "python" changes = {1: 'Y', 3: 'P', 5: 'N'} result = change_multiple_characters(s, changes) print(f"Original: {s}") print(f"Modified: {result}") ... Use string slicing for single character replacement and list conversion for multiple changes.
🌐
Python.org
discuss.python.org › ideas
Str.replace of a set of characters - Ideas - Discussions on Python.org
December 1, 2019 - I would like str.replace, when given a set of characters, to replace occurrence of any of the characters in the set. I.e. 'ASDFGH'.replace(set('SFH'), '') == 'ADG' should then hold. It would mean I would not have to go …
🌐
Brainly
brainly.com › computers and technology › high school › how can you replace multiple characters in a string in python?
[FREE] How can you replace multiple characters in a string in Python? - brainly.com
November 19, 2023 - To replace multiple characters in a Python string, you can use the replace() method multiple times or utilize a loop for efficiency. For more complex replacements, the re module allows pattern replacement using regular expressions.
🌐
Built In
builtin.com › software-engineering-perspectives › python-remove-character-from-string
How to Remove Characters From a String in Python | Built In
An introduction on how to remove characters from a string in Python. | Video: Case Digital · Using replace(), we can replace a specific character. If we want to remove that specific character, we can replace that character with an empty string.