pandas.Series.str.strip

Remove leading and trailing characters.

Strip whitespaces (including newlines) or a set of specified characters from each string in the Series/Index from left and right sides. Equivalent to str.strip().

param to_strip
str or None, default None

Specifying the set of characters to be removed. All combinations of this set of characters will be stripped. If None then whitespaces are removed.

return

Series/Index of objects

Limitations

  • All values in Series equal to None are converted to NaNs.

  • This function may reveal slower performance than Pandas* on user system. Users should exercise a tradeoff

    between staying in JIT-region with that function or going back to interpreter mode.

Examples

Remove leading and trailing characters.
import pandas as pd
from numba import njit


@njit
def series_str_strip():
    series = pd.Series(['1. Ant.  ', '2. Bee!\n', '3. Cat?\t'])

    return series.str.strip('123.!? \n\t')


print(series_str_strip())
$ python ./series/str/series_str_strip.py
0    Ant
1    Bee
2    Cat
dtype: object

See also

Series.str.strip

Remove leading and trailing characters in Series.

Series.str.lstrip

Remove leading characters in Series.

Series.str.strip

Remove trailing characters in Series.