Understanding of the Strip () function in Python

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.