And partition string functions rpartition

Python partition () method

description

Partition () method is used to split the specified string delimiter.

If the string contains the specified delimiter, it returns a tuple of $ 3, the first sub-string is left delimiter, the delimiter character is the second, the third sub-string is right of the separator.

Partition () method is new in version 2.5.

grammar

partition () method syntax:

str.partition(str)

parameter

str: the specified delimiter. 

return value

Returns a 3-tuple element, a first sub-string is left delimiter, the delimiter character is the second, the third sub-string is right of the separator.

Python rpartition () method

description

rpartition () method is similar to Partition () method, which is only from the end of the destination string is the right to start the search delimiter. .

If the string contains the specified delimiter, it returns a tuple of $ 3, the first sub-string is left delimiter, the delimiter character is the second, the third sub-string is right of the separator.

grammar

rpartition () method syntax:

str . rpartition ( str )

parameter

str: the specified delimiter.

 

return value

Returns a 3-tuple element, a first sub-string is left delimiter, the delimiter character is the second, the third sub-string is right of the separator.

Examples

The following examples demonstrate the use of partition () method and rpartition () is used:

>>>url = "https://www.baidu.com/index.php"
>>>url.partition('/')
('https:', '/', '/www.baidu.com/index.php')
>>>url.partition('///')
('https://www.baidu.com/index.php', '', '')
>>>url.rpartition('/')
('https://www.baidu.with /','', 'index.php')
>>>url.rpartition('///')
('', '', 'https://www.baidu.com/index.php')
>>>left, sep, right = url.partition('://')
>>>print(left, right)
https www.baidu.com/index.php

As can be seen from the first example, it is used to split the specified string delimiter, if the string contains the specified delimiter, a 3-membered tuple is returned, the first separator is left substring substring, the second is the separator itself, and the third right of the separator. The second example illustrates, if no specified separator, return is still a 3-tuple element, a first for the entire string, the second and third empty string.

Guess you like

Origin www.cnblogs.com/ilyou2049/p/11108997.html