Python partition string Function

Source: Internet
Author: User

Http://cache.baidu.com/C? M = queue & P = 882a9643d58512a05beace375744cf20 & newp = queue & User = Baidu & fm = SC & query = Python + partition & qid = ff4e5bcf244c9d94 & p1 = 2

Added a new Partition Function in version 2.5. What can it do? Here is a small example:

>>> 'HTTP: // www.donews.net/limodou'.partition ('://')
('HTTP ',': // ', 'www .donews.net/limodou ')
>>> 'File:/a.html '. Partition ('://')
('File:/a.html ',",")

From the first example, we can see that it is used to split the string according to the specified separator. If the string contains the specified separator, a tuple of 3 yuan is returned, the first is the substring on the left of the separator, the second is the separator itself, and the third is the substring on the right of the separator. The second example shows that if the specified separator is not found, a 3-dollar tuple is returned, the first is the entire string, and the second and third are empty strings.

So someone has to ask, what is the difference between it and split (SEP, 1? First, the return value of split may not be a fixed length value. It returns a list. If a list is found, a list of 2 RMB is returned. If no list is found, a list of 1 RMB is returned, for example:

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

At the same time, when it is found, it does not return a separator.

In some cases, partition (SEP) and rpartition (SEP) (matching from right to left) are similar to split (SEP, 1) and rsplit (SEP, 1. However, partition is actually generated to replace find and index, not to replace split. In many cases, we need to first find a location through find and then split it. It is much easier to use partition. For example:

>>> A = 'HTTP: // www.donews.net'
>>> Pos = A. Find ('://')
>>> If POS>-1:
... Print a [: POS], a [POS + 1:]
HTTP www.donews.net

Use partition:

>>> A = 'HTTP: // www.donews.net'
>>> Left, SEP, Right = A. Partition ('://')
>>> Print left, right
HTTP www.donews.net

Is it simpler.

At the same time, in version 2.5, startswith and endswith have changes. Its first parameter can be a tuple. In this way, it is very convenient to judge several situations. For example, to determine the suffix of a file name, if only one value is supported, you may need to split the file first and then judge it. You cannot use endswith, for example:

>>> A = 'a.gif'
>>> Import OS. Path
>>> Ext = OS. Path. splitext (a) [1]
>>> If ext in example '.gif ', '.png', '.bmp ']:
... Print 'found'
...
Found

Now we can:

>>> A = 'a.gif'
>>> If a.endswith(('.gif ', 'png', '.bmp ')):
... Print 'found'
...
Found

Is it much simpler. Note: The above tuple cannot be changed to list. It seems to be mandatory.

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.