Sometimes, we want to delete a character from a string using Python.
In this article, we’ll look at how to delete a character from a string using Python.
How to delete a character from a string using Python?
To delete a character from a string using Python, we can use the string’s replace
method.
For instance, we write:
oldstr = 'EXAMPLE'
newstr = oldstr.replace("M", "")
print(newstr)
We call oldstr.replace
with 'M'
and an empty string to remove ‘M’ from 'EXAMPLE'
and return the new string
Therefore, newstr
is 'EXAPLE'
.
Conclusion
To delete a character from a string using Python, we can use the string’s replace
method.