In python, the string partition() method is useful to search for the specified string and split the given string into a tuple object with three elements based on the specified string.
The three elements in the tuple object will contain the following substring parts.
Following is the pictorial representation of string partition()
method functionality in python.
If you observe the above diagram, we are searching for the "to
" string in the given string "Welcome to Tutlane
" using the partition
function in python. The partition()
method has returned the tuple object by splitting the specified string into three parts.
Following is the syntax of defining a string partition method in python to search and split the given string into a tuple object with three elements.
The string partition()
method will accept one string parameter to search for the specified substring in the given string.
Following is the example of using the partition()
function in python to search and split the given string.
If you observe the above example, we used the partition()
function with one parameter to search for the specified string.
The above python partition method example will return the result as shown below.
If the specified string is not found in the given string, the string partition() method will return the tuple object with three elements.
Following is the example of the string partition() method in python.
The above python string partition() method example will return the result as shown below.
This is how you can use the string partition method in python to search for the specified string and split the given string into a tuple object based on the specified string in python.