Python string | strip()
strip() is an inbuilt function in Python programming language that returns a copy of the string with both leading and trailing characters removed (based on the string argument passed).
string.strip([chars]) Parameter: There is only one optional parameter in it: 1)chars - a string specifying the set of characters to be removed. If the optional chars parameter is not given, all leading and trailing whitespaces are removed from the string. Return Value: Returns a copy of the string with both leading and trailing characters removed.
geeks for geeks geeks for geeks for
geeks for geeks for
Working of above code :
We first construct a string str1 = ‘geeks for geeks’
Now we call strip method over str1 and pass str2 = ‘ekgs’ as argument.
Now python interpreter trace str1 from left.It remove the character of str1 if it is present in str2.
Otherwise it stops tracing.
Now python interpreter trace str1 from right. It remove the character of str1 if it is present in str2.
Otherwise it stop tracing.
Now at last it returns the resultant string.
When we call strip() without argument, it removes leading and trailing spaces.
geeks for geeks
geeks for geeks
Given a string remove occurrence of word “the” from the beginning and the end.
the King has the largest army in the entire world the
King has the largest army in the entire world
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