Traps for strip () in Python

Source: Internet
Author: User

When using Python, a pit of the Strip () command was found.

The previous understanding of strip (x) is to remove the "x" string that is contained at the beginning and end, if not included, without removing it.

Here's a pit: Python's handling of string x is a set, not a fixed-order string. That is, the X is split into a set of a single letter, and if the string of the Strip () contains any of the characters of that set in the left and right side, it will be strip () dropped. Examples are as follows:

>>> a= "abc_1213" >>> a.strip (' abc_ ') ' 1213 ' >>> a.strip (' ba_c ') ' 1213 ' >>> a.strip ( ' Zxcvbnmasdfghjklpoiuytrewq ') ' _1213 ' >>> a.strip (' zxcvbnmasdfg_hjklpoiuytrewq ') ' 1213 '

In the beginning, the individual's understanding is the first case. Strip (' abc_ ') will only remove the "abc_" to the left.

After the experiment, it is confirmed that the string to be strip is treated as a set. As long as the left and right characters are inside the set, they are removed one at a time until the first character that is not inside the set is encountered.

Traps for strip () 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.