Categories
Python Answers

How to remove specific characters from a string in Python?

Spread the love

Sometimes, we want to remove specific characters from a string in Python.

In this article, we’ll look at how to remove specific characters from a string in Python.

How to remove specific characters from a string in Python?

To remove specific characters from a string in Python, we call the string translate method.

For instance, we write

line = line.translate(None, '!@#$')

to call translate to remove the characters listed in '!@#$' and assign the returned string back to line.

Conclusion

To remove specific characters from a string in Python, we call the string translate 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 *