NumPy String operations: find() function

numpy.core.defchararray.find() function

The numpy.core.defchararray.find() function returns the lowest index in the string for each element where substring sub is found.

Version: 1.15.0


numpy.core.defchararray.find(a, sub, start=0, end=None)


Name Description Required /
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:

out : ndarray or int - Output array of ints. Returns -1 if sub is not found.


The 'chararray' class exists for backwards compatibility with Numarray, it is not recommended for new development. Starting from numpy 1.4, if one needs arrays of strings, it is recommended to use arrays of 'dtype' 'object_', 'string_' or 'unicode_', and use the free functions in the 'numpy.char' module for fast vectorized string operations.

Some methods will only be available if the corresponding string method is available in your version of Python.

The preferred alias for 'defchararray' is 'numpy.char'.

Example-1: numpy.find() function

>>> import numpy as np
>>> import numpy as np
>>> a = np.char.find('Hello', 'World', start=0, end=None)
>>> a

Pictorial Presentation:

NumPy String operation: find() function

Example-2: numpy.find() function

>>> import numpy as np
>>> np.char.find('Hello World', 'World', start=0, end=None)
>>> np.char.find('Hello World', 'world', start=0, end=None)

Python - NumPy Code Editor:

Previous: count()
Next: index()