Python String | strip method
Start your free 7-days trial now!
Python's str.strip(~)
method returns a string with the specified leading and trailing characters removed. Default is to remove all leading and trailing spaces (i.e. spaces on the left and right).
Parameters
1. chars
| string
| optional
The string specifying the combination of characters to remove. Default is whitespace.
Return value
A string with the specified leading and trailing characters stripped.
Examples
Basic usage
To remove all leading and trailing spaces from " Dog "
:
x = " Dog "x.strip()
'Dog'
Chars parameter
To remove all leading and trailing combinations of ' reS'
from " SkyTowner "
:
y = " SkyTowner "y.strip(' reS')
'kyTown'
Note that all the leading and trailing ' '
, 'r'
, 'e'
, 'S'
from the source string are stripped. The first mismatch occurs at 'k'
at the start of the string and 'n'
at the end of the string.
Case sensitivity
To remove all leading and trailing combinations of 'rs'
from "SkyTowner"
:
z = "SkyTowner"z.strip('rs')
'SkyTowne'
Note that the removal of characters is case sensitive, hence the leading 'S'
is not removed.