NumPy String Functions – String Operations in NumPy
Free NumPy course with real-time projects Start Now!!
NumPy library contains the numpy.char module for performing NumPy string functions. It consists of a set of vectorized string operations. The functions available are similar to the string operations in python. We perform these functions on arrays of string type
NumPy String Functions
NumPy string operations are of three types-
- String operations
- String information
- NumPy String comparisons
1. NumPy String Operations
These are string functions used to return strings after applying functions over the input strings.
a. np.lower() – It converts all the upper case characters in the string to lower case. If there are no upper-case characters, then it returns the original string.
import numpy as np #lower case print(np.char.lower(['Data', 'Flair']))
Output
b. np.upper()- It converts all the lower case characters in the string to upper case. If there are no lower-case characters, then it returns the original string.
#upper case print(np.char.upper(['Data', 'Flair']))
Output
c. np.split()- This function returns a string after breaking the string, separated by an input separator.
#split print(np.char.split("Data Flair"))
Output
d. np.join()- This is a string method that joins the given string by a particular separator.
#join print(np.char.join("-" ,"DataFlair"))
Output
e. np.strip()- This is useful to strip off all the leading and trailing white spaces from a string.
#strip print(np.char.strip(" DataFlair "))
Output
f. np.capiatlize()- This function is useful to capitalize or upper case the first character of the string.
#capital print(np.char.capitalize("capital"))
Output
g. np.title()- This function helps to convert the first character to upper-case.
#title print(np.char.title("data flair"))
Output
h. np.center()- It returns a new string with specific padding.
i. np.encode()- It returns the encoded string.
j. np.decode()- It returns the decoded string.
k. np.ljust()- It returns left-justified text
l. np.rjust()- It returns right-justified text
2. NumPy String Information
These functions are useful to retain certain information and characters from the input string.
a. np.count()- It returns the count of the passed substring from the entire string
import numpy as np arr=np.array(['aa','ab','bc']) # count print(np.char.count(arr,'a'))
Output
b. np.find()- This function is used to return the lowest index of the substring if present in the input string.
import numpy as np arr=np.array(['aa','ab','bc']) # find print(np.char.find(arr,'a'))
Output
c. np.rfind()- This function returns the highest index of the substring if present in the input string.
import numpy as np arr=np.array(['aa','ab','bc']) # rfind print(np.char.rfind(arr,'a'))
Output
d. np.isnumeric()- This function is used to detect numeric values. It returns “True” if all the string characters are numeric.
import numpy as np arr=np.array(['1','2','3']) # numeric values print(np.char.isnumeric(arr))
Output
e. np.isalpha()- This function is used to detect alphabets. It returns “True” if all the string characters are alphabets.
import numpy as np arr=np.array(['aa','ab','bc']) # alphabets print(np.char.isalpha(arr))
Output
f. np.isdecimal()- This function is used to detect decimal values. It returns “True” if all the string characters are decimal numbers.
import numpy as np arr=np.array(['1','2','3']) # decimal values print(np.char.isdecimal(arr))
Output
g. np.isdigit()- This function is used to detect digits. It returns “True” if all the string characters are digits.
import numpy as np arr=np.array(['1','b','3']) # digits print(np.char.isdigit(arr))
Output
h. np.islower()- This function is used to find lower case values. It returns “True” if all the string characters are lower case.
import numpy as np arr=np.array(['a','b','C']) # lowercase values print(np.char.islower(arr))
Output
i. np.isupper()- This function is used to detect upper case. It returns “True” if all the string characters are upper case.
import numpy as np arr=np.array(['a','b','C']) # upppercase values print(np.char.isupper(arr))
Output
j. np.isspace()- This function returns true if all the characters of the string are white spaces otherwise returns false.
import numpy as np arr=np.array(['a',' ','C']) # white space print(np.char.isspace(arr))
Output
k. np.istitle()- This function returns true only if the string is title cased.
import numpy as np arr=np.array(['Xyz Abc']) # title print(np.char.istitle(arr))
Output
3. NumPy String Comparisons
These functions return Boolean values, true or false after making comparisons between two strings
a. np.equal()- This function checks elements wise equivalence. It returns true only if all the elements are equal.
import numpy as np # equal print(np.char.equal('aa','aa'))
Output
b. np.not_equal()- This function checks whether two strings are equal or not.
import numpy as np # not equal print(np.char.not_equal('ab','aa'))
Output
c. np.greater()- This function returns true if the first string is greater than the second string.
import numpy as np # greater than print(np.char.greater('abc','aa'))
Output
d. np.less()- This function returns true if the first string is less than the second string.
import numpy as np # less than print(np.char.less('a','aaxx'))
Output
e. np.greater_equal()- This function returns true if the first string is greater than or equal to the second string.
import numpy as np # greater than equal to print(np.char.greater_equal('a','aa'))
Output
f. np.less_equal()- This function returns true if the first string is less than or equal to the second string.
import numpy as np # less than equal to print(np.char.less_equal('a','aaxx'))
Output
Summary
There is a vast range of NumPy string functions applicable to strings. The functions are of three categories, string operations, string information, and string comparisons.
The char module is useful to apply these string functions. The string functions increase is based on the in-built string functions in Python.
Did you like this article? If Yes, please give DataFlair 5 Stars on Google