NumPy strings.rpartition()
The numpy.strings.rpartition()
function splits each element of an array at the last occurrence of a specified separator and returns a 3-tuple containing:
- The part before the separator
- The separator itself
- The part after the separator
If the separator is not found, the first two elements of the tuple will be empty strings, and the third element will contain the entire original string.
Syntax
numpy.strings.rpartition(a, sep)
Parameters
Parameter | Type | Description |
---|---|---|
a | array-like | Input array containing string elements. |
sep | array-like | The separator used to split each string at the last occurrence. |
Return Value
Returns a tuple of three arrays:
- The first array contains the part before the separator.
- The second array contains the separator itself.
- The third array contains the part after the separator.
Examples
1. Splitting Strings with a Common Separator
In this example, we split various fruit names at the last occurrence of the letter 'a'
.
import numpy as np
# Define an array of fruit names
fruits = np.array(["apple", "banana", "cherry", "grape", "mango"])
# Define the separator
separator = "a"
# Perform right partitioning
before, sep, after = np.strings.rpartition(fruits, separator)
# Print the results
print("Before separator:", before)
print("Separator:", sep)
print("After separator:", after)
Output:
Before separator: ['apple' 'banan' 'cherry' 'grape' 'mang']
Separator: ['', 'a', '', '', 'a']
After separator: ['' 'a' 'cherry' 'grape' 'o']

2. When the Separator is Not Found
In this case, if the separator is absent in a string, the first two parts of the tuple will be empty strings, and the entire original string will be in the third part.
import numpy as np
# Define an array of words
words = np.array(["orange", "blueberry", "kiwi"])
# Separator that is not present in every word
separator = "z"
# Perform right partitioning
before, sep, after = np.strings.rpartition(words, separator)
# Print the results
print("Before separator:", before)
print("Separator:", sep)
print("After separator:", after)
Output:
Before separator: ['', '', '']
Separator: ['', '', '']
After separator: ['orange' 'blueberry' 'kiwi']

3. Using a Multi-Character Separator
In this example, we use a multi-character separator "rr"
to partition the strings.
import numpy as np
# Define an array of words
words = np.array(["cherry", "strawberry", "raspberry"])
# Multi-character separator
separator = "rr"
# Perform right partitioning
before, sep, after = np.strings.rpartition(words, separator)
# Print the results
print("Before separator:", before)
print("Separator:", sep)
print("After separator:", after)
Output:
Before separator: ['che' 'strawbe' 'raspbe']
Separator: ['rr' 'rr' 'rr']
After separator: ['y' 'y' 'y']

4. Partitioning Email Addresses
We extract the username and domain by splitting email addresses at the last occurrence of "@"
.
import numpy as np
# Define an array of email addresses
emails = np.array(["john@example.com", "alice@yahoo.com", "bob@gmail.com"])
# Separator for partitioning
separator = "@"
# Perform right partitioning
before, sep, after = np.strings.rpartition(emails, separator)
# Print the results
print("Username:", before)
print("Separator:", sep)
print("Domain:", after)
Output:
Username: ['john' 'alice' 'bob']
Separator: ['@' '@' '@']
Domain: ['example.com' 'yahoo.com' 'gmail.com']
