Pandas Series str | strip method
Start your free 7-days trial now!
Pandas Series str.strip(~) method removes the combination of the specified characters that appear in the front and back of each string in the Series.
Note that a new Series is returned, and the original is kept intact.
Parameters
1. to_strip | str or None | optional
The combination of the characters of to_strip that appears at the front or back of each string will be removed. For instance, if to_strip="ab", then abA and baA will both become A.
By default, leading and trailing whitespaces (including newlines and tabs) will be removed.
Return Value
A new Series or Index.
Examples
Basic usage
By default, strip(~) removes leading and trailing whitespaces (including newlines and tabs):
0 1 a a2 a3 adtype: object
To confirm that the whitespaces have indeed been stripped, we print the length of each string:
0 01 32 13 1dtype: int64
Specifying to_strip
To remove a combination of the characters "ab" at the front and back:
s.str.strip("ab")
0 A1 Adtype: object
Here, notice how "baA" got stripped down to "A" - this is because a combination of the specified characters is stripped ("ab" and "ba" in this case).