Signup/Sign In

Python String partition()

The python partition() method, as clear from the name of the method, is used to divide the string into smaller parts or we can say the python string partition() method is mainly used for the partition of the string.

  • The partition() method is an inbuilt method in python which is used for string handling and this method always returns a python tuple as its output.

  • There is one parameter named as the separator for this method which is used for the partitioning of the string.

  • As we said above that it returns a tuple in its output, so this tuple comprises mainly three parts: where the first part indicates the part of the string before the separator, second part indicates the separator itself, the third part indicates the part of the string after the separator.

  • This method splits the string whenever there is a first occurrence of the separator.

Python String partition(): Syntax

Below we have a basic syntax of String partition() in Python:

string.partition(separator)

Note: In the above syntax string denotes the value of string variable on which partition() method will be applied and separator is the only parameter of this method.

Python String partition(): Parameters

Given below is a description of the parameter of the partition() function:

  • separator

    It is a parameter of the partition() method which is used to divide the string into two parts. On the first occurrence of this separator in the string the partition() method divides the string into two parts.

Python String partition(): Returned Values

This method mainly returns a tuple and the tuple further has three parts:

  • The first part indicates the part of the string before the separator.

  • The second part indicates the separator itself.

  • The third part indicates the part of the string that is after the separator.

Python String partition(): Basic Example

Below we have an example to show the working of python partition() function:

str1 = "I am a coding lover!"
str2 = "World of science is magic!"

print("Original String: ", str1, "Partition: ", str1.partition('a'))
print("Original String: ", str2, "Partition: ", str2.partition('magic'))

The Output for the above is given below:


Original String: I am a coding lover! Partition: ('I ', 'a', 'm a coding lover!')
Original String: World of science is magic! Partition: ('World of science is ', 'magic', '!')

Python String partition(): Another Example

There is another example where we will pass a separator that is not the part of the given string and then we will see the output for the same. The code snippet for the same is:

str1="Hello I am wonderwoman"
print("Original String: ",str1,"Partitioned tuple: ",str1.partition('are'))

The Output will be:


Original String: Hello I am wonderwoman Partitioned tuple: ('Hello I am wonderwoman', ' ', ' ')

Time for a Live Example!

Let us see a Live example for python partition() where we will use this method in different ways:

Summary

In this tutorial, we studied partition() method of strings in python which is mainly used for the partition of the string.



About the author:
Aspiring Software developer working as a content writer. I like computer related subjects like Computer Networks, Operating system, CAO, Database, and I am also learning Python.