numpy.index() in Python

numpy.core.defchararray.index(arr, substring, start=0, end=None): Finds the lowest index of the sub-string in the specified range But if substring is not found, it raises ValueError.

Parameters:
arr : array-like or string to be searched.
substring : substring to search for.
start, end : [int, optional] Range to search in.

Returns : An integer array with the lowest index of found sub-string, raises ValueError if substring is not found.



Code #1:

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python Program illustrating 
# numpy.char.index() method 
import numpy as np 
  
arr = ['this is geeks for geek']
  
print ("arr : ", arr)
  
print ("\nindex of 'geeks' : ", np.char.index(arr, 'geeks'))

chevron_right


Output:

arr :  ['this is geeks for geek']

index of 'geeks' : [8]

 
Code #2:

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python Program illustrating 
# numpy.char.index() method 
import numpy as np 
  
arr = ['this is geeks for geek']
  
print ("\nindex of 'geeks' : ", np.char.index(arr, 'geeks', start = 2))
print ("\nindex of 'geeks' : ", np.char.index(arr, 'geeks', start = 10))
print ("\nindex of 'geek' : ", np.char.index(arr, 'geek', start = 10))

chevron_right


Output:

index of 'geeks' : [8]
ValueError: substring not found
index of 'geek' : [18]


My Personal Notes arrow_drop_up

Aspire to Inspire before I expire

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.