Partition String Function example in Python tutorial

Source: Internet
Author: User
in version 2.5 New and added a partition called. function , what can it do? To give a small example:

>>> ' Http://www.donews.net/limodou '. Partition ('://')
(' http ', '://', ' Www.donews.net/limodou ')
>>> ' file:/a.html '. Partition ('://')
(' file:/a.html ', ",")


As can be seen from the first example, it is used to split the string according to the specified delimiter, if the string contains the specified delimiter, a tuple of 3 is returned, the first is the substring to the left of the delimiter, the second is the delimiter itself, and the third is the substring to the right of the delimiter. The second example shows that if the specified delimiter is not found, the return is still a tuple of $3, the first is the entire string, and the second and third is an empty string.

So someone asked, what's the difference between it and split (Sep, 1)? First the split returned may not be a fixed-length return value, it returns a list, if found, returns a 2-dollar list, if not found, returns a list of $1, such as:

>>> ' A.B.C '. Split (', ', 1) [' A.B.C ']>>> ' A.B.C '. Split ('. ', 1) [' A ', ' B.C ']

At the same time, it does not return a delimiter in the case where it is found.

In some cases partition (SEP) and rpartition (Sep) (right-to-left matching) are similar to the functions of Split (Sep, 1) and Rsplit (Sep, 1). But partition was actually created to replace Find,index, not to replace split. In many cases, we need to find a location and then split it through find. The use of partition is much more convenient. Such as:

>>> a = ' http://www.donews.net ' >>> pos = A.find ('://') >>> if pos >-1:     ... Print a[:p os], a[pos+1:]http www.donews.net

Instead, use partition:

>>> a = ' http://www.donews.net ' >>> left, sep, right = a.partition ('://') >>> print left, righth TTP Www.donews.net

Isn't it simpler?

Also in version 2.5, StartsWith and endswith have changed, its first parameter can be a tuple. This is very handy when judging a few situations. For example, to determine the filename suffix, originally only support a value, you may want to split, then judge, not endswith, such as:

>>> a = ' a.gif ' >>> import os.path>>> ext = Os.path.splitext (a) [1]>>> if ext in ['. gif ' , '. png ', '. bmp ']: ...     print ' found ' ... found

And now you can:

>>> a = ' a.gif ' >>> if A.endswith (('. gif ', ' png ', '. bmp ')):     ... print ' found ' ... found

is not much easier. Note that the above tuple I changed to list is no, it seems to be mandatory.

There are many interesting things waiting for you to find out.

"Recommended"

1. share a detailed description of the string function (partition) in Python

2. share the instance code of a string function (partition)

3. mysql-Data table partitioning technology partition code sample analysis

Related Article

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.