Function: Strip () Lstrip () Rstrip ()
Function: Remove spaces in a string or specify characters
First, the default usage: Remove space
Str.strip (): Remove spaces on both sides of the string
Str.lstrip (): Remove space to the left of the string
Str.rstrip (): Remove the space to the right of the string
Note: The spaces here contain ' \ n ', ' \ R ', ' \ t ', '
Default Usage Instance
>>> Dodo = "Hello Boy" >>> dodo.strip () ' Hello Boy ' >>> dodo.lstrip () ' Hello Boy ' >>> do Do.rstrip () ' Hello Boy '
Second, remove the specified characters
Str.strip (' Do '): Removes characters specified at both ends of the string
Str.lstrip (' do '): Used to remove the character specified on the left
Str.rstrip (' do '): Used to remove the character specified on the right
Three functions can pass in a parameter ("Do" here, for example), specify the first and last characters to be removed, and the compiler will remove all corresponding characters from both ends until there are no matching characters
Note:
1. When you remove a specified character, you must not have a blank space when you pass in the parameter.
2. A combination of the specified character representation, such as ' do ' means ' dd ', ' do ', ' od ', ' oo ', ' ddd ', ' ooo ', etc.
Removing character instances
>>> Dodo = "Say hello say boy Saaayaaas" >>> dodo.strip (' say ') ' Hello say boy ' >>> dodo.strip (' Y As ') ' Hello say boy '
#当传入的参数中加入空格时
>>> Dodo.strip (' say ') ' Hello say Bo ' >>> dodo.lstrip (' say ') ' Hello say boy Saaayaaas ' >>> Dodo.rstrip (' say ') ' Say hello say boy '