New Tutorials:

# NumPy index() function

In this tutorial, we will cover the `index()` function of the char module in the Numpy library.

The `index()` function is used to perform string search operation in a given array of strings. If we have an array of strings then this function will provide the first index of any substring to be searched, if it is present in the array elements.

• This function is similar to the Numpy find() function but the only difference, between `index()` and `find()` function is that if the substring is not found for any string then the `index()` function will raise a value error exception.

• This function calls `str.index` for each element of the given array.

### Syntax of `index()`:

The syntax required to use this function is as follows:

``numpy.char.index(a, sub, start=0, end=None)``

The above syntax indicates that `index()` function takes 4 parameters as shown above.

Parameters:

Let us now discuss the parameters of this function:

• a
This parameter indicates an array either of strings or Unicode.

• sub
This parameter indicates the substring which is to be searched.

• start
This is an optional parameter indicating the starting index from where the substring should be searched.

• end
This is an optional parameter indicating the ending index upto where the substring should be searched.

Returned Values:

This function will return an output array of integer values with the index values for the substring(if found) corresponding to every string element present in the input array. If the substring is not found in all the array elements, then this function will throw a ValueError.

## Example 1:

Now we will take a look at the code snippet where we want to search a substring that exists in the input strings and see the output for the same:

``````import numpy as np

ar = np.array(['bBaBaBb', 'baAbaB', 'abBABba'])
print ("The Input array :\n ", ar)

output = np.char.index(ar, sub ='b')
print ("The Output array:\n", output) ``````

The Input array :
['bBaBaBb' 'baAbaB' 'abBABba']
The Output array:
[0 0 1]

## Example 2:

Below we have the code snippet where we want to search a substring that does not exist in the input strings and see the output for the same:

``````import numpy as np

ar = np.array(['bBaBaBb', 'baAbaB', 'abBABba'])
print ("The Input array :\n ", ar)

output = np.char.index(ar, sub ='c')
print ("The Output array:\n", output)
``````

The Input array :
['bBaBaBb' 'baAbaB' 'abBABba']
In this tutorial we covered the `index()` function of the Numpy library. We covered how it is used with its syntax and values returned by this function along with some simple code examples.