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: rpartition() function

numpy.core.defchararray.rpartition() function

The numpy.core.defchararray.rpartition() function is used to partition (split) each element around the right-most separator.
Calls str.rpartition element-wise.
For each element in a, split the element as the last 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.rpartition(a, sep)

Parameter:

Name Description Required /
Optional
a: array-like of str or unicode Input array Required
sep: int or unicode Right-most separator to split each element in array. Required

Return value:

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

p>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-1: numpy.rpartition() function

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

Output:

Original string:
Python Exercises, Practice, Solution

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

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

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

Pictorial Presentation:

NumPy String operation: rpartition() function

Example-2: numpy.rpartition() function

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

Output:

Original string:
Python Exercises, Solution,  Practice, Solution

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

Pictorial Presentation:

NumPy String operation: rpartition() function

Python - NumPy Code Editor:

Previous: rjust()
Next: rsplit()