Python String strip() Method
The Python strip() method is used to remove a specified number of characters (by default, spaces) or sequences of characters from the beginning and end of a string.
Note: This method only removes characters at the beginning or end of a string, not the middle part.
Python String strip() Syntax
Strip() method syntax.
str.strip([chars]);
Python String strip() Parameters
- chars – removes the sequence of characters specified at the beginning and end of the string.
Python String strip() Return Value
Returns the new string generated by removing the sequence of characters specified at the beginning and end of the string.
Python String strip() Example#1
The following example demonstrates the use of the strip() function.
#!/usr/bin/python3
str = "*****this is **string** example....for apidemos.com*****"
print (str.strip( '*' )) # Specify the string *
Output:
From the result, it can be noticed that the middle part of the characters are not deleted.
Python String strip() Example#2
The following example demonstrates the deletion of a character in the specified character sequence whenever the header and footer contain it.
#!/usr/bin/python3
str = "123abcapidemos321"
print (str.strip( '12' )) # The character sequence is 12
Output: