Horje
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
# 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
# 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
# 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' 'f:o:r']
FUNCTIONDESCRIPTION
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.rpartitionPartition (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
# 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
# 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
# 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
FUNCTIONDESCRIPTION
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
# 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
import numpy as np

# Comparing a string element-wise using not_equal() method
a = np.char.not_equal('geeks', 'for')

print(a)

Output
True

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

Python
# 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


Reffered: https://www.geeksforgeeks.org


AI ML DS

Related
Numpy | Sorting, Searching and Counting Numpy | Sorting, Searching and Counting
Numpy | ndarray Numpy | ndarray
Advanced Tableau Skills Advanced Tableau Skills
Art of Data Visualization: Essential Tips for Effective Storytelling Art of Data Visualization: Essential Tips for Effective Storytelling
How to Specify Split in a Decision Tree in R Programming? How to Specify Split in a Decision Tree in R Programming?

Type:
Geek
Category:
Coding
Sub Category:
Tutorial
Uploaded by:
Admin
Views:
13