NumPy String: numpy.char.rindex() function
numpy.char.rindex() function
The numpy.char.rindex() returns the highest index in the string at which the substring is found. If the substring is not found it raises a ValueError.
Syntax:
numpy.char.rindex(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 | The start and end parameters are optional and are used to specify the start and end positions of the search. | optional |
Return value:
out : ndarray - Output array of ints.
Example: Finding the rightmost index using numpy.char.rindex()
>>> import numpy as np
>>> x = np.char.rindex('the quick brown fox', 'fox', start=0, end=None)
>>> x
array(16)
In the above example the numpy.char.rindex() function finds the index of the last occurrence of the string 'fox' in the given string 'the quick brown fox'. Here the function returns the value 16, which corresponds to the index of the first character of the rightmost occurrence of the substring 'fox' in the given string.
Pictorial Presentation:
Example: Finding the rightmost index of a substring in a string using numpy.char.rindex()
>>> import numpy as np
>>> y = np.char.rindex('the quick brown fox jumps over the lazy dog', 'over the lazy dog', start=0, end=None)
>>> y
array(26)
In the above example numpy.char.rindex() function finds the rightmost index of the substring 'over the lazy dog' in the string 'the quick brown fox jumps over the lazy dog'. The function returns 26, which is the index of the rightmost occurrence of 'over the lazy dog' in the given string.
Pictorial Presentation:
Python - NumPy Code Editor:
Previous:
rfind()
Next:
startswith()
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://w3resource.com/numpy/string-operations/rindex.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics