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. Here’s an example demonstrating how we can use it to remove a specific character from 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
Removing a all specific text after a word in Python.
A site I wish someone had shown me when I was learning... Drop your code in here and step through it ("Visualize Execution") and watch what it's doing. See if you can figure it out. If not feel free to come back :) https://pythontutor.com/visualize.html More on reddit.com
🌐 r/learnpython
23
9
July 3, 2022
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 remove stuff like (. , ' *) from a string?
You can use isalnum() to remove special characters. The string method .isalnum() returns True or False if a string is all alphanumeric characters. With this we can write a comprehension that loops over the characters in the string and only keep the alphanumeric characters. I think the regex method is more efficient, especially for really big strings, but you have to know all the characters you want to remove, with isalnum you don't. On the flipside you don't have to add another import if you use isalnum. So it depends on the situation for which method you should choose (re vs isalnum). s = "Bob 'hit' a ball, the hit BALL flew far after it was hit." # Remove all special characters unless it is a space # Replace "hit" with "sample" new_s = "".join(char for char in s if char.isalnum() or char == " ").replace("hit", "sample") print(new_s) >>>Bob sample a ball the sample BALL flew far after it was sample More on reddit.com
🌐 r/learnpython
14
1
August 6, 2021
🌐
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.
Top answer
1 of 16
787

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"
🌐
DigitalOcean
digitalocean.com › community › tutorials › python-remove-character-from-string
How to Remove Characters from a String in Python | DigitalOcean
May 31, 2026 - 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 to remove characters at the start, end, or a fixed index. This tutorial walks through each approach ...
🌐
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.
Find elsewhere
🌐
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 regular ex­pres­sion [^a-zA-Z] matches any character that is not a lowercase or uppercase letter. As a result, modified_string is only made up of the letters from the original string. Keep in mind that this removes the spaces between the letters as well.
🌐
Built In
builtin.com › software-engineering-perspectives › python-remove-character-from-string
How to Remove Characters From a String in Python | Built In
Python’s translate() method allows for the removal or replacement of certain characters in a string. Characters can be replaced with nothing or new characters as specified in a dictionary or mapping table. For example, let’s use translate() to remove “$” from the the following string:
🌐
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)
🌐
WsCube Tech
wscubetech.com › resources › python › programs › remove-character-from-string
How to Remove Characters From String in Python? (8 Programs)
October 30, 2025 - Learn how to remove characters from a string in Python with 8 different methods, including examples, output, and explanations. Read now!
🌐
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 - In this post, you’ll learn how to use Python to remove a character from a string. You’ll learn how to do this with the Python .replace() method as well as the Python .translate() method.
🌐
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)
🌐
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 from a string.
🌐
Real Python
realpython.com › python-strip
How to Strip Characters From a Python String – Real Python
February 4, 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.
🌐
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.
🌐
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 - The replace() and transform() methods allow you to remove a character from a Python string. On Career Karma, learn how to remove a character from a string in Python.
🌐
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(i): None for i in '!?'} ) print(my_new_string) # output # Hi I love Python · In the example above, I replaced both ! and ? characters with the value None by using an iterator that looped through the characters I wanted to remove. The translate() method checks whether each character in my_string is equal to an exclamation point or a question mark. If it is, then it gets replaced with None. Hopefully, this article helped you understand how to remove characters from a string in Python using the built-in replace() and translate() string methods.
🌐
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 ...
🌐
SW Hosting
swhosting.com › en › blog › how-to-remove-a-character-from-a-string-in-python
How to remove a character from a string in python - SW Hosting's Blog
April 10, 2024 - In this article, we will explore ... replace method (): A simple way to remove a character from a string is to use Python's replace() de Python method....