.strip
According to the manual , there is the .strip()
method:
minhaString.strip()
If you need to specify characters to be removed, such as spaces only, leaving tabs and line breaks, you can use this:
minhaString.strip(" ")
Note that in this second case, if you specify multiple characters, they are treated individually:
"tomate seco".strip("otm")
Return:
ate sec
.rstrip and .lstrip
If you need to remove only to the left, you have .lstrip()
:
"banana".lstrip( "abc" )
Return:
nana
If you need to remove only to the right, you have .rstrip()
:
"banana".lstrip( "a" )
Return:
banan
The default of .rstrip
and .lstrip
is also removing spacing characters such as tabs and line breaks.
Deprecated
Just to note, this existed as a function:
string.strip( string [, caracteres] )
It worked the same way as described above, but string was the 1st argument of the call, and the second, optional, characters to remove. Likewise, if omitted, it refers to whitespace, tabs, and line breaks.
I'm referring in the past, but it will work until it's actually removed.