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.
🌐
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 › ways-to-remove-ith-character-from-string-in-python
How to Remove Letters From a String in Python - GeeksforGeeks
October 27, 2025 - The replace() method replaces all occurrences of a character with another replacing it with an empty string effectively removes it. ... Since replacement creates a new string, the original remains unchanged.
Find elsewhere
🌐
W3Schools
w3schools.com › python › ref_string_strip.asp
Python String strip() Method
Python Examples Python Compiler ... Python Study Plan Python Interview Q&A Python Training ... The strip() method removes any leading, and trailing whitespaces. Leading means at the beginning of the string, trailing means at ...
🌐
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.
🌐
Real Python
realpython.com › python-strip
How to Strip Characters From a Python String – Real Python
October 22, 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.
🌐
Note.nkmk.me
note.nkmk.me › home › python
Remove a Substring from a String in Python | note.nkmk.me
April 23, 2025 - To remove a substring from the middle of a string, slice and concatenate the parts you want to keep. Concatenate strings in Python (+ operator, join, etc.)
🌐
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…
🌐
DataCamp
datacamp.com › tutorial › python-trim
How to Trim a String in Python: Three Different Methods | DataCamp
February 16, 2025 - Python provides built-in methods to trim strings, making it straightforward to clean and preprocess textual data. These methods include · .strip(): Removes leading and trailing characters (whitespace by default).
🌐
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 › how do i remove a substring from a string by indicating what i am wanting to remove with a string variable?
r/learnpython on Reddit: How do I remove a substring from a string by indicating what I am wanting to remove with a string variable?
July 29, 2022 -

I have been trying to get this one working all day. I have a string that contains a substring that I would like to remove but the typical replace function isn't working in the way I would like to use it. I have the substring I want to remove stored in a string variable while having the string I am modifying in another variable. I tried to use the function like this

inspected = replace(substring, '')

but for some reason I get an error indicating that only one argument was passed rather than two. I think this function only accepts quotes rather than the variables themselves unless I am doing something wrong. I have been searching for this one all day but to no avail.

example

inspected = "iamthewalrus"
substring = "walrus"
inspected = replace(substring,"")

🌐
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.
🌐
W3Schools
w3schools.com › python › python_ref_string.asp
Python String Methods
Python has a set of built-in methods that you can use on strings.
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-remove-substring-list-from-string
Remove substring list from String - Python - GeeksforGeeks
July 12, 2025 - Explanation: remove_substrings iterates through each substring in list a and removes all its occurrences from the given string s using replace(). It returns the cleaned string after all specified substrings have been removed.
🌐
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!
🌐
Python documentation
docs.python.org › 3 › reference › lexical_analysis.html
2. Lexical analysis — Python 3.14.7 documentation
A Python program is read by a parser. Input to the parser is a stream of tokens, generated by the lexical analyzer(also known as the tokenizer). This chapter describes how the lexical analyzer prod...
🌐
Django
docs.djangoproject.com › en › 6.0 › ref › models › querysets
QuerySet API reference | Django documentation | Django
As explained in Limiting QuerySets, a QuerySet can be sliced, using Python’s array-slicing syntax. Slicing an unevaluated QuerySet usually returns another unevaluated QuerySet, but Django will execute the database query if you use the “step” parameter of slice syntax, and will return a list.