![]() |
VOOZH | about |
strip() method in Python removes all leading and trailing whitespace by default. You can also specify a set of characters to remove from both ends. It returns a new string and does not modify the original.
Let's take an example to remove whitespace from both ends of a string.
GeeksforGeeks
Explanation: s.strip() removes spaces at the start and end of the string.
s.strip(chars)
Parameters:
Return Type: A new string with specified characters removed from both ends.
We can also use custom characters from the beginning and end of a string. This is useful when we want to clean up specific unwanted characters such as symbols, punctuation, or any other characters that are not part of the core string content
GeeksforGeeks
Explanation:
We can also remove the leading and trailing newline characters (\n) from a string.
Geeks for Geeks
Explanation: Removes newline characters \n from both ends of the string.
Hello World
Explanation: Removes both tabs (\t) and spaces from the start and end of the string.
Related articles:newline characters (\n)