EN
Python - trim whitespace from string
0
points
In this article, we would like to show you how to trim whitspace from a string in Python.
Quick solution:
text = " abc "
print(text.strip()) # "abc"
Practical example
In this example, we use strip()
method to trim whitespace from the beginning and from the end of the string.
text1 = " abc"
text2 = "abc "
text3 = " abc "
print(text1.strip()) # abc
print(text2.strip()) # abc
print(text3.strip()) # abc
Output:
abc
abc
abc