Numpy | String Operations

This module is used to perform vectorized string operations for arrays of dtype numpy.string_ or numpy.unicode_. All of them are based on the standard string functions in Python’s built-in library.

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 program explaining
# numpy.lower() function

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 program explaining
# numpy.split() function

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 program explaining
# numpy.join() function

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', 'g:e:e:k:s']

 

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 program explaining
# numpy.count() function

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'))

Output :

[1, 0, 1]
[0, 1, 0]

 
numpy.rfind() : This function returns the highest index of the substring if found in given string. If not found then it returns -1.


# Python program explaining
# numpy.rfind() function

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 program explaining
# numpy.isnumeric() function

import numpy as np


# counting a substring
print(np.char.isnumeric('geeks'))

# counting a substring
print(np.char.isnumeric('12geeks'))

Output :

False
False

 

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 program explaining
# numpy.equal() function

import numpy as np

# comparing a string elementwise
# using equal() method
a=np.char.equal('geeks','for')

print(a)

Output :

False

 
numpy.not_equal(): This function checks whether two string is unequal or not.


# Python program explaining
# numpy.unequal() function

import numpy as np

# comparing a string elementwise
# using unequal() method
a=np.char.unequal('geeks','for')

print(a)

Output :

True

 
numpy.greater(): This function checks whether string1 is greater than string2 or not.


# Python program explaining
# numpy.greater() function

import numpy as np

# comparing a string elementwise
# using greater() method
a=np.char.greater('geeks','for')

print(a)

Output :

True

 

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.



  • Last Updated : 25 Jan, 2024

Share your thoughts in the comments
Similar Reads