In this article, we would like to show you how to trim whitspace from a string in Python.
text = " abc " print(text.strip()) # "abc"
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
abc abc abc