Numpy String Functions & Operations (original) (raw)
Last Updated : 23 Jan, 2025
NumPy String functions belong to the numpy.char module and are designed to perform element-wise operations on arrays. These functions can help to handle and manipulate string data efficiently.
Table of Content
In this article, we’ll explore the various string functions provided by NumPy along with their examples.
**String Operations
- **numpy.lower()****:** This function returns the lowercase string from the given string. It converts all uppercase characters to lowercase. If no uppercase characters exist, it returns the original string. Python `
import numpy as np
converting to lowercase
print(np.char.lower(['GEEKS', 'FOR']))
converting to lowercase
print(np.char.lower('GEEKS'))
`
Output
['geeks' 'for'] geeks
- **numpy.split() : This function returns a list of strings after breaking the given string by the specified separator. Python `
import numpy as np
splitting a string
print(np.char.split('geeks for geeks'))
splitting a string
print(np.char.split('geeks, for, geeks', sep = ','))
`
Output
['geeks', 'for', 'geeks'] ['geeks', ' for', ' geeks']
- **numpy.join() : This function is a string method and returns a string in which the elements of sequence have been joined by str separator. Python `
import numpy as np
splitting a string
print(np.char.join('-', 'geeks'))
splitting a string
print(np.char.join(['-', ':'], ['geeks', 'for']))
`
Output
g-e-e-k-s ['g-e-e-k-s' 'f:o:r']
FUNCTION | DESCRIPTION |
---|---|
**numpy.strip() | It is used to remove all the leading and trailing spaces from a string. |
**numpy.capitalize() | It converts the first character of a string to capital (uppercase) letter. If the string has its first character as capital, then it returns the original string. |
**numpy.center() | It creates and returns a new string which is padded with the specified character.. |
**numpy.decode() | It is used to convert from one encoding scheme, in which argument string is encoded to the desired encoding scheme. |
**numpy.encode() | Returns the string in the encoded form |
**numpy.ljust() | Return an array with the elements of a left-justified in a string of length width. |
**numpy.rjust() | For each element in a, return a copy with the leading characters removed. |
**numpy.strip() | For each element in a, return a copy with the leading and trailing characters removed. |
**numpy.lstrip() | Convert angles from degrees to radians. |
**numpy.rstrip() | For each element in a, return a copy with the trailing characters removed. |
**numpy.partition() | Partition each element in a around sep. |
**numpy.rpartition | Partition (split) each element around the right-most separator. |
**numpy.rsplit() | For each element in a, return a list of the words in the string, using sep as the delimiter string. |
**numpy.title() | It is used to convert the first character in each word to Uppercase and remaining characters to Lowercase in string and returns new string. |
**numpy.upper() | Returns the uppercased string from the given string. It converts all lowercase characters to uppercase.If no lowercase characters exist, it returns the original string. |
**String Information
- **numpy.count() : This function returns the number of occurrences of a substring in the given string. Python `
import numpy as np
a=np.array(['geeks', 'for', 'geeks'])
counting a substring
print(np.char.count(a,'geek'))
counting a substring
print(np.char.count(a, 'fo'))
`
- **numpy.rfind() : This function returns the highest index of the substring if found in given string. If not found then it returns -1. Python `
import numpy as np
a=np.array(['geeks', 'for', 'geeks'])
counting a substring
print(np.char.rfind(a,'geek'))
counting a substring
print(np.char.rfind(a, 'fo'))
`
Output
[ 0 -1 0] [-1 0 -1]
- **numpy.isnumeric() : This function returns “True” if all characters in the string are numeric characters, Otherwise, It returns “False”. Python `
import numpy as np
counting a substring
print(np.char.isnumeric('geeks'))
counting a substring
print(np.char.isnumeric('12geeks'))
`
FUNCTION | DESCRIPTION |
---|---|
**numpy.find() | It returns the lowest index of the substring if it is found in given string. If its is not found then it returns -1. |
**numpy.index() | It returns the position of the first occurrence of substring in a string |
**numpy.isalpha() | It returns “True” if all characters in the string are alphabets, Otherwise, It returns “False”. |
**numpy.isdecimal() | It returns true if all characters in a string are decimal. If all characters are not decimal then it returns false. |
**numpy.isdigit() | It returns “True” if all characters in the string are digits, Otherwise, It returns “False”. |
**numpy.islower() | It returns “True” if all characters in the string are lowercase, Otherwise, It returns “False”. |
**numpy.isspace() | Returns true for each element if there are only whitespace characters in the string and there is at least one character, false otherwise. |
**numpy.istitle() | Returns true for each element if the element is a titlecased string and there is at least one character, false otherwise. |
**numpy.isupper() | Returns true for each element if all cased characters in the string are uppercase and there is at least one character, false otherwise. |
**numpy.rindex() | Returns the highest index of the substring inside the string if substring is found. Otherwise it raises an exception. |
**numpy.startswith() | Returns True if a string starts with the given prefix otherwise returns False. |
**String Comparison
- numpy.equal(): This function checks for string1 == string2 elementwise. Python `
import numpy as np
using equal() method
a=np.char.equal('geeks','for')
print(a)
`
- **numpy.not_equal(): This function checks whether two string is unequal or not. Python `
import numpy as np
Comparing a string element-wise using not_equal() method
a = np.char.not_equal('geeks', 'for')
print(a)
`
**numpy.greater(): This function checks whether string1 is greater than string2 or not.
Python `
import numpy as np
comparing a string elementwise
a=np.char.greater('geeks','for')
print(a)
`
FUNCTION | DESCRIPTION |
---|---|
**numpy.greater_equal() | It checks whether string1 >= string2 or not. |
**numpy.less_equal() | It checks whether string1 is <= string2 or not. |
**numpy.less() | It check whether string1 is lesser than string2 or not. |
Convenience class
**array: Creates a new NumPy array from a list or similar data structure.
Python `
import numpy as np arr = np.array([1, 2, 3, 4]) # Creating a NumPy array print(arr)
`
**chararray: Creates a NumPy array specialized for handling string operations.
Python `
import numpy as np char_arr = np.chararray((2, 3)) # Creating a 2x3 character array char_arr[:] = 'Hello' # Assigning string values print(char_arr)
`
**asarray: Converts input data into a NumPy array, avoiding unnecessary copying if already an array.
Python `
import numpy as np lst = [1, 2, 3, 4] arr = np.asarray(lst) # Converting list to NumPy array print(arr)
`
FUNCTIONS | DESCRIPTION |
---|---|
chararray | NumPy array for string operations, providing vectorized string processing capabilities. |
array | Creates a NumPy array from input data |
asarray | Converts input data into a NumPy array without copying |