Python strip() Method: Complete Guide
In Python, the strip() function is used to remove specified characters (by default, whitespace) from both ends of a string. You can use the strip() function in the following way:
- Remove leading and trailing spaces using the default space character.
s = " hello world "
print(s.strip()) # Output: "hello world"
- Specify the characters to be removed:
s = "!!!hello world!!!"
print(s.strip("!")) # Output: "hello world"
- Remove specified set of characters.
s = "!!!hello world!!!"
print(s.strip("!d")) # Output: "hello worl"
It is important to note that the strip() function will return a new string and the original string will not be modified.