Python String partition() Method Tutorial

In this section, we will learn what String partition() method is and how to use it in Python.

What is String partition() Method in Python?

The Python string partition() method, as the name of the method suggests, is used to partition a string value.

Basically, this method takes a string value as its argument and then looks for the first occurrence of this value in the target string (the one that invoked the method). Now when it found this value, it will return a tuple that contains 3 items:

  • The first item is the sub-string of the target string that comes before the specified value (the argument of the method).
  • The second item is the specified string value (The argument).
  • The third item is the sub-string of the string value that comes after the specified value (the argument of the method).

Python String partition() Method Syntax:


String partition() Method Parameter

The method takes one argument and that is the string value we want to look for its first occurrence in the target string.

String partition() Method Return Value

The return value of this method is a tuple with 3 items that mentioned at the beginning of this section.

Note: if the target string value didn’t have the specified value (the argument of the method) then the tuple that returns from this method will have the entire string value as its first item and the second and third items will be only an empty string.

Example: using python string partition() method

s1 = "My name is John Doe "



('My ', 'name', ' is John Doe ')

Top Technologies