Strip in Python is used to remove the first and last characters of a string. Similarly, lstrip is used to remove the characters on the left , and rstrip is used to remove the characters on the right .
All three functions can be passed a parameter that specifies the leading and trailing characters to be removed.
It should be noted that a character array is passed in , and the compiler removes all corresponding characters at both ends until there are no matching characters, such as:
theString
=
'saaaay yes no yaaaass'
print
theString.strip(
'say'
)
|
theString is sequentially stripped of the characters in the array ['s', 'a', 'y'] until the characters are not in the array. Therefore, the output result is:
yes no
is relatively simple, the principle of lstrip and rstrip is the same.
Note: When no parameter is passed in, the leading and trailing spaces are removed by default .
theString
=
'saaaay yes no yaaaass'
print
theString.strip(
'say'
)
print
theString.strip(
'say '
)
#say后面有空格
print
theString.lstrip(
'say'
)
print
theString.rstrip(
'say'
)
|
operation result:
yes no
es no
yes no yaaaass
saaaay yes no
Source: https://www.cnblogs.com/pylemon/archive/2011/05/18/2050179.html