Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Python partition() method

Python partition() method

Python Strings Python Strings


Description

The partition() method splits a string based on the specified separator.

If the string contains the specified separator, a 3-tuple is returned: the first element is the substring to the left of the separator, the second is the separator itself, and the third is the substring to the right of the separator.

The partition() method was added in version 2.5.

Syntax

partition() method syntax:



str.partition(str)

Parameters

str: the specified separator.

Return Value

Returns a 3-tuple: the first element is the substring to the left of the separator, the second is the separator itself, and the third is the substring to the right.

Example

The following example shows how to use the partition() method:

Example (Python 2.0+)

#!/usr/bin/python

str

=

"

www.runoob.com

"

print

str

.

partition

(

"

.

"

)

The output is:



('www', '.', 'runoob.com')


Python Strings Python Strings