Categories
Python Answers

How to delete a character from a string using Python?

Spread the love

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.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *