Python String partition() method

Python String Partition() Method

In this article, we will have a look over the functioning of the Python string partition() method. Python String has introduced a large number of built-in functions to manipulate the input string/data.

Getting started with Python String partition()

Python String partition() method is used to split the input string at the very initial/first occurrence of the input separator.

Syntax:

input_string.partition(separator)

Explanation:

As soon as the interpreter spots the separator provided as the argument in the input string, the Python string partition() function does the rest of the work. It splits up the string into three sections within a tuple:

  • The portion of the string which is before the separator
  • The separator delimiter
  • The string which is after the separator

Example 1:

inp_str = "Evan and Norah are good friends and they study in the same college."

res1 = inp_str.partition('friends')
print(res1,'\n') 

In the above snippet of code, the inp_str is partitioned at the separator delimiter: friends.

Output:

('Evan and Norah are good ', 'friends', ' and they study in the same college.') 

Example 2:

inp_str = "Evan and Norah are good friends and they study in the same college."

res2 = inp_str.partition('black')
print(res2,'\n') 


In the above snippet of code, we have tried to split the string at the separator: black.

But, as we all can observe, the string ‘black’ isn’t present in the input string, so in that case, the function returns a tuple containing the entire input string and two empty strings.

Output:

('Evan and Norah are good friends and they study in the same college.', '', '') 

Example 3:

inp_str = "Evan and Norah are good friends and they study in the same college."

res3 = inp_str.partition('and') 
print(res3,'\n') 

In the above example, the separator ‘and‘ occurs twice in the input string. In this case, the partition() function splits the input string around the first occurrence of the input separator.

Output:

('Evan ', 'and', ' Norah are good friends and they study in the same college.') 

Python NumPy partition() method

NumPy module provides us with numpy.partition() method to split up the input array accordingly.

The numpy.partition() method splits up the input array around the nth element provided in the argument list such that,

  • As soon as the numpy.partition() method is called, it first creates a copy of the input array and sorts the array elements
  • Elements smaller than the nth element are placed before it
  • Elements equal to or larger than the nth element are placed after it

Syntax:

numpy.partition(input_array, nth position, axis=-1, kind_of_sort='introselect', order=None)
  • nth position: The index of the element around which the partition needs to be performed.
  • kind_of_sort: The kind of sorting you want to perform. The default value is ‘introselect‘.
  • axis: It is the axis along which the elements would be sorted. The default value is -1.

Example:

import numpy

inp = numpy.array([10, 90, 0, 50, 12, 100, -87]) 
print ("Elements of input array before partition:\n", inp) 

res = numpy.partition(inp, 1) 
print ("Elements of array after partition:\n", res) 

In the above piece of code, the partition() method creates a copy of the input array and sorts it internally.

After sorting the input array looks like: -87, 0, 10, 12, 50, 90, 100.

Out of it, the partition is performed around the element at the first position of the sorted array i.e. 0.

After which, all the elements less than 0 are placed before/to the left of 0 and all the elements greater than 0 are placed to the right of the separator element (0).

Note: The order of elements appearing in the array is undefined.

Output:

Elements of input array before partition:
 [ 10  90   0  50  12 100 -87]
Elements of array after partition:
 [-87   0  90  50  12 100  10]

Python Pandas partition() method

Pandas module provides us with Series.str.partition() method to split the string around the input delimiter.

Syntax:

Series.str.partition(delimiter='', expand=True/False)
  • delimiter: It contains the separator around which the data values are to be partitioned.
  • expand: If false, it returns the separated string as a tuple. Else, it returns the values in the two separate columns around the delimiter. The default value is True.

Input csv file:

Input file
Input file

Example:

import pandas

 
res = pandas.read_csv("C:\\Users\\HP\\Desktop\\Book1.csv") 


res["Details"]= res["Details"].str.partition(":",False) 


print(res)

Output:

Output Partition
Output Partition

Conclusion

In this article, we have understood the working of the Python partition() method under different scenarios.


References

  • Python String partition() method