In Python, strings are immutable, so you have to create a new string. You have a few options of how to create the new string. If you want to remove the 'M' wherever it appears:

newstr = oldstr.replace("M", "")

If you want to remove the central character:

midlen = len(oldstr) // 2
newstr = oldstr[:midlen] + oldstr[midlen+1:]

You asked if strings end with a special character. No, you are thinking like a C programmer. In Python, strings are stored with their length, so any byte value, including \0, can appear in a string.

Answer from Ned Batchelder on Stack Overflow
🌐
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.
Discussions

Remove specific characters from a string in Python - Stack Overflow
I'm trying to remove specific characters from a string using Python. This is the code I'm using right now. Unfortunately, it appears to do nothing to the string. for char in line: if char in &q... More on stackoverflow.com
🌐 stackoverflow.com
Is there a cleaner way to delete characters from a string?
s = s[:i] + s[i+1:] More on reddit.com
🌐 r/pythontips
18
0
March 6, 2023
How do I remove a substring from a string by indicating what I am wanting to remove with a string variable?
replace is a method of str so by calling it by itself it's treating the first argument as the str instance. It's equivalent to substring.replace(''), which, as you can see from the error message you're getting, is missing an argument. You need: your_string.replace(substring, '') More on reddit.com
🌐 r/learnpython
10
30
July 29, 2022
Use regex to remove words in pandas dataframe that are less than 3 characters
Don't you want to sub in a space or something? df.column.replace(value=' ', regex=r'\b[a-z]{1,3}\b') not an empty string? 'blue in shell' should become 'blue shell' not 'blueshell' ? now I'll need to look up what a raw string is... tl;dr: it tells python not to convert \b into an escaped character, but rather interpret it as raw text exactly as it is, as a slash followed by a b. You'll see this used a lot for regex or for filepaths. More on reddit.com
🌐 r/learnpython
10
1
March 11, 2021
🌐
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 ...
🌐
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 - s1 = "Python" # Index of the character to remove idx = 3 # Use a loop to build a new string without the character res = ''.join(char for i, char in enumerate(s1) if i != idx) print(res)
🌐
DigitalOcean
digitalocean.com › community › tutorials › python-remove-character-from-string
How to Remove Characters from a String in Python | DigitalOcean
May 31, 2026 - To remove characters from a string in Python, build a new string because str objects are immutable. Use str.replace() for a single character or substring, str.translate() or str.maketrans() to drop several characters in one pass, re.sub() for pattern-based removal, and slicing when you need ...
🌐
IONOS
ionos.com › digital guide › websites › web development › removing characters from strings in python
How to remove a character from a string in Python - IONOS
December 10, 2024 - The translate() method is a built-in function in Python used for advanced character re­place­ment and trans­la­tion in strings. It provides an efficient way to replace char­ac­ters using a table of trans­la­tions. original_string = "Hello, World! Remove vowels." translation_table = str.maketrans(dict.fromkeys('aeiouAEIOU', '*')) modified_string = original_string.translate(translation_table) print(original_string) # Output: Hello, World!
Find elsewhere
🌐
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.
🌐
datagy
datagy.io › home › python posts › python strings › python: remove a character from a string (4 ways)
Python: Remove a Character from a String (4 Ways) • datagy
December 17, 2022 - Let’s take a look at how we can iterate over a string of different characters to remove those characters from a string in Python. The reason we don’t need to loop over a list of strings is that strings themselves are iterable. We could pass in a list of characters, but we don’t need to. Let’s take a look at an example where we want to replace both the ? and the ! characters from our original string: a_string = 'h?ello, my name is nik! how are you?' for character in '!?': a_string = a_string.replace(character, '') print(a_string) # hello, my name is nik how are you
🌐
GeeksforGeeks
geeksforgeeks.org › python › remove-a-substring-in-python
How to Remove a Substring in Python? - GeeksforGeeks
July 23, 2025 - The simplest and most common method to remove a substring from a string is by using the replace() method. This method returns a new string where all occurrences of the specified substring are replaced (in this case, with an empty string).
🌐
DEV Community
dev.to › lifeportal20002010 › python-string-manipulation-every-way-to-delete-specific-characters-1913
Python String Manipulation: Every Way to Delete Specific Characters - DEV Community
January 28, 2026 - To clean up unwanted characters (like spaces or newlines) only at the start or end of a string, use the strip family of methods: ... text = " Hello Python! " print(f"strip : '{text.strip()}'") # 'Hello Python!' print(f"lstrip: '{text.lstrip()}'") ...
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-remove-string-from-string-list
Python - Remove String from String List - GeeksforGeeks
March 24, 2023 - remove() generally removes the first occurrence of K string and we keep iterating this process until no K string is found in list. ... # Python 3 code to demonstrate # Remove K String from String List # using remove() # initializing list test_list ...
🌐
Real Python
realpython.com › python-strip
How to Strip Characters From a Python String – Real Python
February 4, 2025 - By default, Python’s .strip() method removes whitespace characters from both ends of a string. To remove different characters, you can pass a string as an argument that specifies a set of characters to remove.
Top answer
1 of 16
786

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"
🌐
Stack Abuse
stackabuse.com › python-how-to-remove-a-character-from-a-string
Python: How to Remove a Character from a String
October 14, 2023 - For example, any_string.replace('a', 'b') will replace all occurrences of 'a' in any_string with the character 'b'. To remove a character from a string via replace(), we'll replace it with an empty character: original_string = "stack abuse" # Removing character 'a' and replacing with an empty character new_string = original_string.replace('a', '') print("String after removing the character 'a':", new_string) ... Python strings have a translate() method which replaces the characters with other characters specified in a translation table.
🌐
Flexiple
flexiple.com › python › python-remove-character-from-string
How to remove a character from a string in Python? - Flexiple
March 14, 2022 - Replace is one of the most common methods used to remove a character from a string in Python. Although, replace() was intended to replace a new character with an older character - using "" as the new character can be used to remove a character ...
🌐
Career Karma
careerkarma.com › blog › python › python remove character from string: a guide
Python Remove Character from String: A Guide | Career Karma
December 1, 2023 - What if we want to remove all periods (full stops), underscores, and exclamation marks from our string? Create a new Python file and paste in this code: username = input("Choose a username: ") disallowed_characters = "._!" for character in disallowed_characters: username = username.replace(character, "") print("Your username is: " + username)
🌐
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.
🌐
freeCodeCamp
freecodecamp.org › news › how-to-remove-a-specific-character-from-a-string-in-python
How to Remove a Specific Character from a String in Python
December 7, 2022 - Python!" my_new_string = my_string.translate( { ord("!"): None } ) print(my_new_string) # output # Hi I love Python · In the example above, I used the ord() function to return the Unicode value associated with the character I wanted to replace, which in this case was !. Then, I mapped that Unicode value to None - another word for nothing or empty - which makes sure to remove it.