Python partition() Method

Python 字符串Python String


Description

The partition() method is used to split a string according to the specified separator.

If the string contains the specified separator, it returns a 3-element tuple: the first is the substring before the separator, the second is the separator itself, and the third is the substring after the separator.

The partition() method was added in version 2.5.

Syntax

The syntax of the partition() method is:

str.partition(str)

Parameters

str: The specified separator.

Return Value

Returns a 3-element tuple: the first is the substring before the separator, the second is the separator itself, and the third is the substring after the separator.

Example

The following example demonstrates the use of the partition() method:

Example (Python 2.0+)

#!/usr/bin/python str = "www.example.com" print str.partition(".")

The output is:

('www', '.', 'example.com')

Python 字符串Python String

Other Extensions