What is the numpy.char.partition() function in Python?
Overview
The char.partition() function in Python is used to partition each element in an input array using a separator.
Syntax
char.partition(a, sep)
Parameter(s)
The char.partition() function takes the following parameter values:
a: Represents the input array.sep: Represents the separator used to split the elements in the input array.
Return value
The char.partition() function returns an array of strings or Unicode, depending on the input type in the input array.
Example
import numpy as np# creating an array of stringsmyarray = np.array(["This is my house"])# creating separator charactersep ='my'# Implementing the char.partition() function and printing our result.print(np.char.partition(myarray, sep))
Explanation
- Line 1: We import the
numpymodule. - Line 4: We create an array of strings
myarrayusing thearray()function. - Line 7: We create a separator variable
septhat we use to partition the elements in the array. - Line 10: We implement the
np.char.partition()function on the variablesmyarrayandsep, and print the result.