The strip() method in-built function of Python is used to remove all the leading and trailing spaces from a string.
chars(optional): Character or a set of characters, that needs to be removed from the string.
Returns: A copy of the string with both leading and trailing characters stripped.
Using strip() method:
- In case the character of the string to the left doesn’t match with the characters in the char parameter, the method stops removing the leading characters.
- In case the character of the string to the right doesn’t match with the characters in the char parameter, the method stops removing the trailing characters.
Geeks for Geeks for Geeks for Geeks
Geeks for Geeks Geeksforgeeks
The following code shows an application of strip() in python.
Length before strip() 17 Length after removing spaces 15
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course