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 arguement.
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
- Python String Methods | Set 3 (strip, lstrip, rstrip, min, max, maketrans, translate, replace & expandtabs())
- Python String | strip()
- Python - Strip front and rear Punctuations from given String
- numpy string operations | strip() function
- Python | Pandas Series.str.strip(), lstrip() and rstrip()
- Wand strip() function - Python
- String slicing in Python to check if a string can become empty by recursive deletion
- String slicing in Python to rotate a string
- Python | Check if a given string is binary string or not
- Python | Check if given string can be formed by concatenating string elements of list
- Python | Check if string ends with any string in given list
- Python | Sorting string using order defined by another string
- Python | Merge Tuple String List values to String
- Python | Sort each String in String list
- Python | Convert List of String List to String List
- Python - Length of shortest string in string list
- Python - Remove front K characters from each string in String List
- Python - Remove String from String List
- Python | Delimited String List to String Matrix
- String Alignment in Python f-string
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.