Signup/Sign In
LAST UPDATED: DECEMBER 10, 2020

Numpy char.partition() function

In this tutorial, we will cover char.partition() function in the char module of the Numpy Library in Python.

The numpy.char.partition() function is mainly used to partition each element in a given input array around the specified separator.

This function calls the str.partition in an element-wise manner.

For each element in a given array, this function will split the element on encountering the first occurrence of the given separator string. This function will return 3 strings that contains part before the separator, the separator itself, and the part after the separator. In case if the separator is not found then the returned 3 strings will contain the string itself, followed by two empty strings.

Syntax of char.partition():

The syntax required to use this function is as follows:

numpy.char.partition(a, sep)

The above syntax indicates that partition() function takes two parameters.

Parameters:

Let us discuss the above-given parameters of this function:

  • a
    This parameter indicates the input array of strings.

  • sep
    This parameter is used to indicate the separator that is used to split each string element in the input array.

Returned Values:

This function will return the output array of string that depends on the input type. The output array will mainly have an extra dimension having 3 elements per input element.

Example 1:

The code snippet is as follows where we will use partition() function:

import numpy as np 

a = "StudyTonight-Education Simplified"
sep ='None'

print("The Input is:")
print(a)
out = np.char.partition(a, sep) 
	
print ("The Output is :")
print(out)

In the above example as the value of separator is None so it will give 3 strings in output i.e original string and two empty strings. Let us take a look at the output for the same.

Output:

numpy char.partition() function example

Example 2:

In the code below we will provide a value for sep parameter and then see the result of partiton:

import numpy as np 

a = "StudyTonight-Education Simplified"
sep ='Ed'

print("The Input is:")
print(a)
out = np.char.partition(a, sep) 
	
print ("The Output is :")
print(out)

Output:

numpy char.partition() function example

Summary

In this tutorial we learned about the partition() function of the char module in Numpy Library. We covered how it is used with its syntax and values returned by this function along with a few code examples.



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.