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.