
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Return Highest Index of Substring in a Range using Python rindex
Return the highest index in the string where substring sub is found using the numpy.char.rindex() method in Python Numpy. The method returns the output array of ints. Raises ValueError if sub is not found. The first parameter is the input array. The second parameter is the substring to be searched. The third and fourth parameter are optional arguments, wherein start and end are interpreted as in slice notation.
Steps
At first, import the required library −
import numpy as np
Create a One-Dimensional array of strings −
arr = np.array(['KATIE', 'KATE', 'BRATleukATp'])
Displaying our array −
print("Array...\n",arr)
Get the datatype −
print("\nArray datatype...\n",arr.dtype)
Get the dimensions of the Array −
print("\nArray Dimensions...\n",arr.ndim)
Get the shape of the Array −
print("\nOur Array Shape...\n",arr.shape)
Get the number of elements of the Array −
print("\nNumber of elements in the Array...\n",arr.size)
Return the highest index in the string where substring sub is found using the numpy.char.rindex() method −
print("\nResult (rindex() method)...\n",np.char.rindex(arr, 'AT', start = 1, end= 9))
Example
import numpy as np # Create a One-Dimensional array of strings arr = np.array(['KATIE', 'KATE', 'BRATleukATp']) # Displaying our array print("Array...\n",arr) # Get the datatype print("\nArray datatype...\n",arr.dtype) # Get the dimensions of the Array print("\nArray Dimensions...\n",arr.ndim) # Get the shape of the Array print("\nOur Array Shape...\n",arr.shape) # Get the number of elements of the Array print("\nNumber of elements in the Array...\n",arr.size) # Return the highest index in the string where substring sub is found using the numpy.char.rindex() method in Python Numpy # The method returns the output array of ints. Raises ValueError if sub is not found. print("\nResult (rindex() method)...\n",np.char.rindex(arr, 'AT', start = 1, end= 9))
Output
Array... ['KATIE' 'KATE' 'BRATleukATp'] Array datatype... <U11 Array Dimensions... 1 Our Array Shape... (3,) Number of elements in the Array... 3 Result (rindex() method)... [1 1 2]
Advertisements