NumPy String: numpy.char.rfind() function
numpy.char.rfind() function
The numpy.char.rfind() function returns the highest index of the first occurrence of a substring in the string from the right. It returns -1 if the substring is not found.
The function can be used to search a substring from the end of the string.
Syntax:
numpy.char.rfind(a, sub, start=0, end=None)
Parameters:
Name | Description | Required / Optional |
---|---|---|
a: array_like of str or unicode | Input an array_like of string or unicode | Required |
sub: str or unicode | Input string or unicode | Required |
start, end: int | Optional arguments start and end are interpreted as in slice notation. | Optional |
Return value:
Output array of ints. Return -1 on failure.
Example: Finding the rightmost occurrence of a substring in a string using numpy.char.rfind()
>>> import numpy as np
>>> x = np.char.rfind('The Quick Brown Fox', 'Fox', start=0, end=None)
>>> x
array(16)
In the above example, the function returns the index of the rightmost occurrence of the substring 'Fox' in the string 'The Quick Brown Fox'. The index value is 16, which means that the substring 'Fox' is found starting from the 16th position from the left end of the string. If the substring is not found, the method returns -1.
Pictorial Presentation:
Example: Finding the position of the last occurrence of a substring in a string using numpy.char.rfind()
>>> import numpy as np
>>> y = np.char.rfind('the quick brown fox jumps over the lazy dog', 'over the lazy dog', start=0, end=None)
>>> y
array(26)
In the above code numpy.char.rfind() function is used to find the position of the last occurrence of the substring 'over the lazy dog' in the string 'the quick brown fox jumps over the lazy dog', starting from the beginning of the string (index 0) and ending at the end of the string (None). The output is an array with the index of the last occurrence of the substring, which is 26.
Pictorial Presentation:
Python - NumPy Code Editor:
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://www.w3resource.com/numpy/string-operations/rfind.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics