w3resource

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

Syntax:

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

Parameter:

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 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.

Note:

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
array(-1)

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)
array(6)
>>> np.char.find('Hello World', 'world', start=0, end=None)
array(-1)

Python - NumPy Code Editor:

Previous: count()
Next: index()