1. Strip () function
Function prototypes
Declaration: S is a string, RM is a sequence of characters to be deleted
S.strip (RM): Remove the characters from the beginning and end of the s string in the RM delete sequence
S.lstrip (RM): Remove the characters from the beginning of the S string in the RM delete sequence
S.rstrip (RM): Delete the character at the end of the S string in the RM delete sequence
Now analyze the S.strip (RM) function.
- Now suppose s= ' ABCD '
Then S.strip (' BD ') ————-> ' abc '
- And the results of S.strip (' BA ') and S.strip (' AB ') are the same, all are ' CDs '
- and S.strip (' BD ') got the result ' abc ', so I don't understand
- So I continued to try and find S.strip (' BAC ') ———-> ' d '
A lot of the blog is so that the next, and then there is no explanation in the end is how to work, why the cause of this, I do not know is too simple so others have not been explained or I am too clumsy, not to understand.
The reason I understand this is as follows: S.strip (RM) first checks whether the beginning and end characters in the string s are in Rm, removes the characters from them if they exist, and continues to check that the first and last characters appear in the RM after the character is removed, and returns the final result.
The above may be said to be more abstract, the above example S.strip (' BA ') For example, went through a few steps
- The first step: the string s= ' ABCD ' first checks if the first and second characters appear in the rm= ' BA ', finds that the initial character ' a ' exists in the rm= ' BA ', and then removes the ' a ' character from ' ABCD ' to get the ' BCD ' string
- Second step: Continue to check that the resulting string ' BCD ' is present in the rm= ' BA ', found that the first character ' B ' exists, then the ' BCD ' character ' B ' is removed from it, to get the ' CD ' string
- The third step: continue to check the resulting string ' CD ' in the first and second characters are small fox in rm= ' BA ', found no, then return it, end.
The individual humble opinion, if not correct or not, if not correct, welcome to criticize
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Understanding of the Strip () function in Python