NumPy – String Functions:
In Numpy, we can handle the string operations with provided functions. some of which we can discuss here.
add: This will return element-wise string concatenation for two arrays of str.
Example:
>>> x=np.array(("iam a numpy"))
>>> y=np.array(("program"))
>>> np.char.add(x,y)
array('iam a numpyprogram',dtype='<U18')multiply: This will return element-wise string multiple concatenation.
Example:
>>> import numpy as np
>>> x=np.char.multiply("numpy",5)
>>> x
array('numpynumpynumpynumpynumpy', dtype='<U25')
>>> print(x)
numpynumpynumpynumpynumpycapitalize: This will return a copy of string with first character of each element capitalized.
Example:
>>> import numpy as np
>>> x=np.char.capitalize("numpy")
>>> x
array('Numpy', dtype='<U5')
>>> print(x)
Numpysplit: This will return a list of words in the string.
Example:
>>> import numpy as np
>>> x=np.char.split("iam a numpy program")
>>> print(x)
['iam', 'a', 'numpy', 'program']lower: This will return an array with elements converted to lowercase.
Example:
>>> import numpy as np
>>> x=np.char.lower("NUMPY")
>>> print(x)
numpyupper: This will return an array with elements converted to uppercase.
Example:
>>> import numpy as np
>>> x=np.char.upper("numpy")
>>> print(x)
NUMPYequal: This will return a element-wise comparision.
Example:
>>> x=np.char.equal("iam","numpy")
>>> print(x)
Falsenot_equal: This will return a element-wise comparision.
Example:
>>> x=np.char.not_equal("iam","numpy")
>>> print(x)
Truecount: This will return an array with the number of non-overlapping occurances of substring in the range.
Example:
>>> x=np.array(['bet','abet','alphabet']) >>> x array(['bet', 'abet', 'alphabet'],dtype='<U8') >>> np.char.count(x,'bet') array([1, 1, 1]) >>> np.char.count(x,'abet') array([0, 1, 1]) >>> np.char.count(x,'alphabet') array([0, 0, 1])
isnumeric: This will return true if there is only numeric characters in the element.
Example:
>>> np.char.isnumeric('bet')
array(False, dtype=bool)rfind: This will return the highest index in the string where substring is found.
Example:
>>> x=np.array(['bet','abet','alphabet']) >>> x array(['bet', 'abet', 'alphabet'],dtype='<U8') >>> np.char.rfind(x,'abet') array([-1, 0, 4]) >>> np.char.rfind(x,'bet') array([0, 1, 5]) >>> np.char.rfind(x,'alphabet') array([-1, -1, 0])