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 strings
myarray = np.array(["This is my house"])
# creating separator character
sep ='my'
# Implementing the char.partition() function and printing our result.
print(np.char.partition(myarray, sep))

Explanation

  • Line 1: We import the numpy module.
  • Line 4: We create an array of strings myarray using the array() function.
  • Line 7: We create a separator variable sep that we use to partition the elements in the array.
  • Line 10: We implement the np.char.partition() function on the variables myarray and sep, and print the result.

Free Resources