Please note, this is a STATIC archive of website www.w3resource.com from 19 Jul 2022, cach3.com does not collect or store any user information, there is no "phishing" involved.
w3resource

NumPy: partition() function

numpy.core.defchararray.partition() function

The numpy.core.defchararray.partition() function is used to partition each element in a around sep.
Calls str.partition element-wise.
For each element in a, split the element as the first occurrence of sep, and return 3 strings containing the part before the separator, the separator itself, and the part after the separator. If the separator is not found, return 3 strings containing the string itself, followed by two empty strings.

Version: 1.15.0

Syntax:

numpy.core.defchararray.partition(a, sep)

Parameter:

Name Description Required /
Optional
a: array_like, {str, unicode} Input array. Required
sep: {str, unicode} Separator to split each string element in a. Required

Return value:

out : ndarray, {str, unicode} Output array of str or unicode, depending on input type. The output array will have an extra dimension with 3 elements per input element.

Note:

The 'chararray' class exists for backwards compatibility with Numarray, it is not recommended for new development. Starting from numpy 1.4, if one needs arrays of strings, it is recommended to use arrays of 'dtype' 'object_', 'string_' or 'unicode_', and use the free functions in the 'numpy.char' module for fast vectorized string operations.

Some methods will only be available if the corresponding string method is available in your version of Python.

The preferred alias for 'defchararray' is 'numpy.char'.

Example: numpy.partition() function

import numpy as np
x = "Python Exercises, Practice, Solution"
print("Original string:")
print(x)
print("\nHere separator is 'Practice'")
print(np.char.partition(x, 'Practice'))
print("\nHere separator is 'Exercises'")
print(np.char.partition(x, 'Exercises'))
print("\nHere separator is ','")
print(np.char.partition(x, ','))

Output:

Original string:
Python Exercises, Practice, Solution

Here separator is 'Practice'
['Python Exercises, ' 'Practice' ', Solution']

Here separator is 'Exercises'
['Python ' 'Exercises' ', Practice, Solution']

Here separator is ','
['Python Exercises' ',' ' Practice, Solution'] 

Pictorial Presentation:

NumPy String operation: partition() function

Pictorial Presentation:

NumPy String operation: partition() function

Python - NumPy Code Editor:

Previous: lstrip()
Next: replace()