Strings in Python are immutable (can't be changed). Because of this, the effect of line.replace(...) is just to create a new string, rather than changing the old one. You need to rebind (assign) it to line in order to have that variable take the new value, with those characters removed.

Also, the way you are doing it is going to be kind of slow, relatively. It's also likely to be a bit confusing to experienced pythonators, who will see a doubly-nested structure and think for a moment that something more complicated is going on.

Starting in Python 2.6 and newer Python 2.x versions *, you can instead use str.translate, (see Python 3 answer below):

line = line.translate(None, '!@#$')

or regular expression replacement with re.sub

import re
line = re.sub('[!@#$]', '', line)

The characters enclosed in brackets constitute a character class. Any characters in line which are in that class are replaced with the second parameter to sub: an empty string.

Python 3 answer

In Python 3, strings are Unicode. You'll have to translate a little differently. kevpie mentions this in a comment on one of the answers, and it's noted in the documentation for str.translate.

When calling the translate method of a Unicode string, you cannot pass the second parameter that we used above. You also can't pass None as the first parameter. Instead, you pass a translation table (usually a dictionary) as the only parameter. This table maps the ordinal values of characters (i.e. the result of calling ord on them) to the ordinal values of the characters which should replace them, or—usefully to us—None to indicate that they should be deleted.

So to do the above dance with a Unicode string you would call something like

translation_table = dict.fromkeys(map(ord, '!@#$'), None)
unicode_line = unicode_line.translate(translation_table)

Here dict.fromkeys and map are used to succinctly generate a dictionary containing

{ord('!'): None, ord('@'): None, ...}

Even simpler, as another answer puts it, create the translation table in place:

unicode_line = unicode_line.translate({ord(c): None for c in '!@#$'})

Or, as brought up by Joseph Lee, create the same translation table with str.maketrans:

unicode_line = unicode_line.translate(str.maketrans('', '', '!@#$'))

* for compatibility with earlier Pythons, you can create a "null" translation table to pass in place of None:

import string
line = line.translate(string.maketrans('', ''), '!@#$')

Here string.maketrans is used to create a translation table, which is just a string containing the characters with ordinal values 0 to 255.

Answer from intuited on Stack Overflow
Top answer
1 of 16
788

Strings in Python are immutable (can't be changed). Because of this, the effect of line.replace(...) is just to create a new string, rather than changing the old one. You need to rebind (assign) it to line in order to have that variable take the new value, with those characters removed.

Also, the way you are doing it is going to be kind of slow, relatively. It's also likely to be a bit confusing to experienced pythonators, who will see a doubly-nested structure and think for a moment that something more complicated is going on.

Starting in Python 2.6 and newer Python 2.x versions *, you can instead use str.translate, (see Python 3 answer below):

line = line.translate(None, '!@#$')

or regular expression replacement with re.sub

import re
line = re.sub('[!@#$]', '', line)

The characters enclosed in brackets constitute a character class. Any characters in line which are in that class are replaced with the second parameter to sub: an empty string.

Python 3 answer

In Python 3, strings are Unicode. You'll have to translate a little differently. kevpie mentions this in a comment on one of the answers, and it's noted in the documentation for str.translate.

When calling the translate method of a Unicode string, you cannot pass the second parameter that we used above. You also can't pass None as the first parameter. Instead, you pass a translation table (usually a dictionary) as the only parameter. This table maps the ordinal values of characters (i.e. the result of calling ord on them) to the ordinal values of the characters which should replace them, or—usefully to us—None to indicate that they should be deleted.

So to do the above dance with a Unicode string you would call something like

translation_table = dict.fromkeys(map(ord, '!@#$'), None)
unicode_line = unicode_line.translate(translation_table)

Here dict.fromkeys and map are used to succinctly generate a dictionary containing

{ord('!'): None, ord('@'): None, ...}

Even simpler, as another answer puts it, create the translation table in place:

unicode_line = unicode_line.translate({ord(c): None for c in '!@#$'})

Or, as brought up by Joseph Lee, create the same translation table with str.maketrans:

unicode_line = unicode_line.translate(str.maketrans('', '', '!@#$'))

* for compatibility with earlier Pythons, you can create a "null" translation table to pass in place of None:

import string
line = line.translate(string.maketrans('', ''), '!@#$')

Here string.maketrans is used to create a translation table, which is just a string containing the characters with ordinal values 0 to 255.

2 of 16
356

Am I missing the point here, or is it just the following:

string = "ab1cd1ef"
string = string.replace("1", "") 

print(string)
# result: "abcdef"

Put it in a loop:

a = "a!b@c#d$"
b = "!@#$"
for char in b:
    a = a.replace(char, "")

print(a)
# result: "abcd"
Discussions

How to delete a character from a string using Python - Stack Overflow
There is a string, for example. EXAMPLE. How can I remove the middle character, i.e., M from it? I don't need the code. I want to know: Do strings in Python end in any special character? Which is a More on stackoverflow.com
🌐 stackoverflow.com
How do you replace or delete characters from a string with python?
I’m wondering how to replace or remove specific letters in a string with python. I’m working on a level editor where the script would create the level layout from a grid of characters in a string. Especially, how to remove spaces and tabs. More on blenderartists.org
🌐 blenderartists.org
4
0
November 17, 2020
removing all non-letter characters from a string? ((using regex))
This is very simple with regex. You just need to replace non-words (/\W/ig). So: var string = "lakjsdlkasjdlsaj@£$%^&*klajdlaskjds"; string.replace(/\W/ig, ""); --> "lakjsdlkasjdlsajklajdlaskjds" \w == words, \W == non words. You don't need a loop here as we're using /g which stands for global, so we replace every instance. And just incase I'm using /i as well which ignores the case. As it's regex you can just do /ig and the selectors will stack to ignore case and global. My favorite regex visualiser lives here: https://jex.im/regulex/#!embed=false&flags=ig&re=%5CW More on reddit.com
🌐 r/learnjavascript
10
4
September 28, 2015
Beginner Question: Removing all substrings from string between two characters.
You should really be using a parser combinator library like nom or combine . Also, your given code would be slow if the comment contained a lot of ; characters. It would be faster to write: "This should be printed; This should not be printed" .split(';') .take(1) .collect::>()[0] (also note the ';' instead of ";", Pattern is implemented for char too, use it). If you want to match text like "this is (a (comment) right here)" I think you would have to do it recursively, which honestly is where you'd want to start using a parser combinator. If you don't want to use other people's code, it is not complicated to write a simple parser combinator and doing so is a lot less effort than attempting to write a parser without one. More on reddit.com
🌐 r/rust
15
2
December 21, 2015
🌐
Codecademy
codecademy.com › article › remove-characters-from-a-python-string
How to Remove Characters from a String in Python | Codecademy
In this example, “!” is replaced with an empty string (“”) using replace(), effectively removing it from the string. ... The re.sub() function in Python’s re (Regular Expressions or RegEx) module is used for replacing occurrences of a pattern in a string. Here’s an example demonstrating how we can use it to remove a specific character from a string:
🌐
GeeksforGeeks
geeksforgeeks.org › python › ways-to-remove-ith-character-from-string-in-python
How to Remove Letters From a String in Python - GeeksforGeeks
October 27, 2025 - Let’s explore different methods to remove letters from a string in Python. The replace() method replaces all occurrences of a character with another replacing it with an empty string effectively removes it.
🌐
Medium
medium.com › @Alexander_H › removing-characters-before-after-and-in-the-middle-of-strings-fb4930cce76a
Removing characters before, after, and in the middle of strings | by This Time Is Different | Medium
October 30, 2017 - .split() #splits the string into two tuples around whatever character it was given and deletes that character..lstrip() #strips everything before and including the character or set of characters you say.
Find elsewhere
🌐
W3Schools
w3schools.com › python › ref_string_strip.asp
Python String strip() Method
The strip() method removes any leading, and trailing whitespaces. Leading means at the beginning of the string, trailing means at the end. You can specify which character(s) to remove, if not, any whitespaces will be removed.
🌐
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.
🌐
Blender Artists
blenderartists.org › game engine › game engine support and discussion
How do you replace or delete characters from a string with python? - Game Engine Support and Discussion - Blender Artists Community
November 17, 2020 - I’m wondering how to replace or remove specific letters in a string with python. I’m working on a level editor where the script would create the level layout from a grid of characters in a string. Especially, how to re…
🌐
GeeksforGeeks
geeksforgeeks.org › python › remove-character-in-a-string-at-a-specific-index-in-python
Remove Character in a String at a Specific Index in Python - GeeksforGeeks
July 23, 2025 - Another way is to convert the string into a list, remove the character and then join the list back into a string. ... s1 = "Python" # Index of the character to remove idx = 3 # Convert string to a list li = list(s1) # Remove the character at the specified index li.pop(idx) # Join the list back into a string res = ''.join(li) print(res)
🌐
Mimo
mimo.org › tutorials › python › how-to-remove-a-character-from-a-string-in-python
How to Remove a Character from a String in Python
Learn how to remove a character from a Python string using replace(), slicing, strip(), or filtering so you delete the right characters without changing the rest.
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-removing-unwanted-characters-from-string
Remove Special Characters from String in Python - GeeksforGeeks
July 11, 2025 - This method is both Pythonic and easy to understand, with good performance on medium-sized strings. ... Explanation: list comprehension iterate through each character in the string s and includes only those that are alphanumeric using char.isalnum(). The join() function then combines these characters into a new string. translate() method removes or replaces specific characters in a string based on a translation table.
🌐
Note.nkmk.me
note.nkmk.me › home › python
Remove a Substring from a String in Python | note.nkmk.me
April 23, 2025 - This article explains how to remove a substring (i.e., a part of a string) from a string in Python. Remove a substring by replacing it with an empty stringRemove exact match string: replace()Remove su ...
🌐
Real Python
realpython.com › python-strip
How to Strip Characters From a Python String – Real Python
October 22, 2025 - Python provides several string methods to remove unwanted characters from the beginning or end of a string. While .strip() removes any specified characters from both ends, .lstrip() and .rstrip() allow for targeted character removal on a single side.
🌐
Jinja
jinja.palletsprojects.com › en › stable › templates
Template Designer Documentation — Jinja Documentation (3.1.x)
Use this if you need to display text that might contain such characters in HTML. If the object has an __html__ method, it is called and the return value is assumed to already be safe for HTML. ... A Markup string with the escaped text.
🌐
AskPython
askpython.com › python › string › remove-character-from-string-python
5 Ways to Remove a Character from String in Python - AskPython
August 6, 2022 - input_str = "DivasDwivedi" # Printing original string print ("Original string: " + input_str) result_str = "" for i in range(0, len(input_str)): if i != 3: result_str = result_str + input_str[i] # Printing string after removal print ("String after removal of i'th character : " + result_str) ... str = "Engineering" print ("Original string: " + str) res_str = str.replace('e', '') # removes all occurrences of 'e' print ("The string after removal of character: " + res_str) # Removing 1st occurrence of e res_str = str.replace('e', '', 1) print ("The string after removal of character: " + res_str)
🌐
DataCamp
datacamp.com › tutorial › python-trim
How to Trim a String in Python: Three Different Methods | DataCamp
February 16, 2025 - These methods include · .strip(): Removes leading and trailing characters (whitespace by default). .lstrip(): Removes leading characters (whitespace by default) from the left side of the string.
🌐
Python documentation
docs.python.org › 3 › library › re.html
re — Regular expression operations
Source code: Lib/re/ This module provides regular expression matching operations similar to those found in Perl. Both patterns and strings to be searched can be Unicode strings ( str) as well as 8-...
🌐
freeCodeCamp
freecodecamp.org › news › python-remove-character-from-a-string-how-to-delete-characters-from-strings
Python Remove Character from a String – How to Delete Characters from Strings
March 7, 2022 - In Python you can use the replace() and translate() methods to specify which characters you want to remove from a string and return a new modified string result. It is important to remember that the original string will not be altered because ...