Categories
Python Answers

How to trim whitespace from a string with Python?

Spread the love

Sometimes, we want to trim whitespace from a string with Python.

In this article, we’ll look at how to trim whitespace from a string with Python.

How to trim whitespace from a string with Python?

To trim whitespace from a string with Python, we can use the Python string’s strip method.

For instance, we write:

stripped = ' Hello '.strip()
print(stripped)

to remove the leading and trailing whitespaces from the ' Hello ' string.

Therefore, stripped is 'Hello'.

Conclusion

To trim whitespace from a string with Python, we can use the Python string’s strip 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 *