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
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
How do I delete lines in a text file?

Do you know the line number you want to omit? Also, do you have to use Python? Bash would be a one-liner. For example to delete line 7:

awk 'NR != 7' input.txt > output.txt

In Python, try something like this:

infile = open('input.txt','r').readlines()
with open('output.txt','w') as outfile:
    for index,line in enumerate(infile):
        if index != 7:
            outfile.write(line)

If you don't know the line number(s) and instead want to match a pattern, you might need to use regular expressions or something.

More on reddit.com
🌐 r/learnpython
12
10
August 26, 2014
🌐
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.
🌐
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 - filter() function provides an efficient way to filter out characters based on a condition. It returns an iterator, which can be converted back to a string. ... Works well for both letters and conditions (like removing vowels, digits, etc.). ...
🌐
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.
🌐
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!
🌐
Note.nkmk.me
note.nkmk.me › home › python
Remove a Substring from a String in Python | note.nkmk.me
April 23, 2025 - Use strip() to remove specified leading and trailing characters from a string. Built-in Types - str.strip() — Python 3.13.3 documentation
Find elsewhere
🌐
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)
🌐
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).
🌐
Built In
builtin.com › software-engineering-perspectives › python-remove-character-from-string
How to Remove Characters From a String in Python | Built In
If the pattern isn’t found, the string is returned unchanged,” according to Python’s documentation. If we want to remove specific characters, the replacement string is mentioned as an empty string. Highlighting the characters that will be removed using re.sub(). | Image: Indhumathy Chelliah
🌐
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 - By passing in a non-zero number into this parameter we can specify how many characters we want to remove in Python. This can be very helpful when you receive a string where you only need to remove the first iteration of a character, but others may be valid.
🌐
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 - You can remove a character from a Python string using replace() or translate(). Both these methods replace a character or string with a given value. If an empty string is specified, the character or string you select is removed from the string ...
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"
🌐
Python Pool
pythonpool.com › home › how to › remove characters from a python string
7 Ways to Remove Character From String Python
July 14, 2026 - To remove a character from a string in Python, create a new string with the unwanted character left out.
🌐
AskPython
askpython.com › python › string › remove-character-from-string-python
5 Ways to Remove a Character from String in Python - AskPython
August 6, 2022 - str = "Engineering" print ("Original string: " + str) # Removing char at pos 3 # using slice + concatenation res_str = str[:2] + str[3:] print ("String after removal of character: " + res_str) ... In this technique, every element of the string is converted to an equivalent element of a list, after which each of them is joined to form a string excluding the particular character to be removed.
🌐
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 - Then, specify the group of characters you want to remove (in this case, the ! and ? characters), along with the characters you want to replace them with. In this case, the replacement is an empty character: import re my_string = "Hi!? I!? love!? Python!?" my_new_string = re.sub('[!?]',"",my_string) print(my_new_string) # output # Hi I love Python
🌐
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 - The most common way to remove a character from a string in Python is with the replace() method. We'll explore replace(), translate() and a custom manual approach to removing a character from a string.
🌐
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 › remove-from-string-in-python-how-to-remove-characters-from-a-string
Remove From String in Python – How to Remove Characters from a String
January 20, 2023 - This works because every character in a string has an index. So, you can use that indexing with slicing to extract or remove some characters from a string. In Python, it’s also possible to remove one or more characters from a string with regular expressions.
🌐
Spark By {Examples}
sparkbyexamples.com › home › python › remove character from string python
Remove Character From String Python - Spark By {Examples}
May 21, 2024 - How to remove character/characters from a String in Python? Removing characters from a string involves deleting one or more characters from the original