w3resource

Pandas Series: str.lstrip() function

Series-str.lstrip() function

The str.lstrip() function is used to remove leading and trailing characters.

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

Syntax:

Series.str.lstrip(self, to_strip=None)
Pandas Series: str.lstrip() function

Parameters:

Name Description Type/Default Value Required / Optional
to_strip Specifying the set of characters to be removed. All combinations of this set of characters will be stripped. If None then whitespaces are removed. str or None
Default Value: None
Required

Returns: Series/Index of objects

Example:

Python-Pandas Code:

import numpy as np
import pandas as pd
s = pd.Series(['1. Dog.  ', '2. Cow!\n', '3. Bee?\t', np.nan])
s

Output:

0    1. Dog.  
1    2. Cow!\n
2    3. Bee?\t
3          NaN
dtype: object

Python-Pandas Code:

import numpy as np
import pandas as pd
s = pd.Series(['1. Dog.  ', '2. Cow!\n', '3. Bee?\t', np.nan])
s.str.strip()

Output:

0    1. Dog.
1    2. Cow!
2    3. Bee?
3        NaN
dtype: object

Python-Pandas Code:

import numpy as np
import pandas as pd
s = pd.Series(['1. Dog.  ', '2. Cow!\n', '3. Bee?\t', np.nan])
s.str.lstrip('123.')

Output:

0     Dog.  
1     Cow!\n
2     Bee?\t
3        NaN
dtype: object

Python-Pandas Code:

import numpy as np
import pandas as pd
s = pd.Series(['1. Dog.  ', '2. Cow!\n', '3. Bee?\t', np.nan])
s.str.rstrip('.!? \n\t')

Output:

0    1. Dog
1    2. Cow
2    3. Bee
3       NaN
dtype: object

Python-Pandas Code:

import numpy as np
import pandas as pd
s = pd.Series(['1. Dog.  ', '2. Cow!\n', '3. Bee?\t', np.nan])
s.str.strip('123.!? \n\t')

Output:

0    Dog
1    Cow
2    Bee
3    NaN
dtype: object
Pandas Series: str.lstrip() function

Previous: Series-str.lower() function
Next: Series-str.pad() function



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://w3resource.com/pandas/series/series-str-lstrip.php