Numpy char.partition() function
In this tutorial, we will cover
char.partition() function in the char module of the Numpy Library in Python.
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.
The syntax required to use this function is as follows:
The above syntax indicates that
partition() function takes two parameters.
Let us discuss the above-given parameters of this function:
This parameter indicates the input array of strings.
This parameter is used to indicate the separator that is used to split each string element in the input array.
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.
The code snippet is as follows where we will use
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.
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)
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.