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:
xxxxxxxxxx
1
text = " abc "
2
print(text.strip()) # "abc"
In this example, we use strip()
method to trim whitespace from the beginning and from the end of the string.
xxxxxxxxxx
1
text1 = " abc"
2
text2 = "abc "
3
text3 = " abc "
4
5
print(text1.strip()) # abc
6
print(text2.strip()) # abc
7
print(text3.strip()) # abc
Output:
xxxxxxxxxx
1
abc
2
abc
3
abc