The following functions are used to perform vectorized string operations on arrays with dtype of numpy.string_ or numpy.unicode_. They are based on the standard string functions in Python's built-in library.
These functions are defined in the character array class (numpy.char).
| Function | Description |
|---|---|
add() |
Concatenates strings element-wise from two arrays |
multiply() |
Returns a string after element-wise multiple concatenation |
center() |
Centers a string |
capitalize() |
Converts the first character of the string to uppercase |
title() |
Converts the first character of each word in the string to uppercase |
lower() |
Converts array elements to lowercase |
upper() |
Converts array elements to uppercase |
split() |
Splits a string by a specified delimiter and returns an array of lists |
splitlines() |
Returns a list of lines in the elements, split by line breaks |
strip() |
Removes specific characters from the beginning or end of an element |
join() |
Joins elements in an array by a specified delimiter |
replace() |
Replaces all occurrences of a substring in a string with a new string |
decode() |
Calls str.decode on each array element sequentially |
encode() |
Calls str.encode on each array element sequentially |
numpy.char.add()
The numpy.char.add() function concatenates strings element-wise from two arrays.
Example
import numpy as np
print('Concatenating two strings:')
print(np.char.add(['hello'], [' xyz']))
print('n')
print('Concatenation example:')
print(np.char.add(['hello', 'hi'], [' abc', ' xyz']))
The output is:
Concatenating two strings:
['hello xyz']
Concatenation example:
['hello abc' 'hi xyz']
numpy.char.multiply()
The numpy.char.multiply() function performs multiple concatenations.
Example
import numpy as np
print(np.char.multiply('Tutorial ', 3))
The output is:
Tutorial Tutorial Tutorial
numpy.char.center()
The numpy.char.center() function is used to center a string, padding it with a specified character on the left and right.
Example
import numpy as np
print(np.char.center('Tutorial', 20, fillchar='*'))
The output is:
******* Tutorial *******
numpy.char.capitalize()
The numpy.char.capitalize() function converts the first character of the string to uppercase:
Example
import numpy as np
print(np.char.capitalize('tutorial'))
The output is:
Tutorial
numpy.char.title()
The numpy.char.title() function converts the first character of each word in the string to uppercase:
Example
import numpy as np
print(np.char.title('i like tutorial'))
The output is:
I Like Tutorial
numpy.char.lower()
The numpy.char.lower() function converts each element of the array to lowercase. It calls str.lower on each element.
Example
import numpy as np
print(np.char.lower(['TUTORIAL', 'GOOGLE']))
print(np.char.lower('TUTORIAL'))
The output is:
['tutorial' 'google']
tutorial
numpy.char.upper()
The numpy.char.upper() function converts each element of the array to uppercase. It calls str.upper on each element.
Example
import numpy as np
print(np.char.upper(['tutorial', 'google']))
print(np.char.upper('tutorial'))
The output is:
['TUTORIAL' 'GOOGLE']
TUTORIAL
numpy.char.split()
The numpy.char.split() function splits a string by a specified delimiter and returns an array. By default, the delimiter is a space.
Example
import numpy as np
print(np.char.split('i like tutorial?'))
print(np.char.split('www.', sep='.'))
The output is:
['i', 'like', 'tutorial?']
['www', 'tutorial', 'com']
numpy.char.splitlines()
The numpy.char.splitlines() function splits a string by line breaks and returns an array.
Example
import numpy as np
print(np.char.splitlines('in like tutorial?'))
print(np.char.splitlines('ir like tutorial?'))
The output is:
['i', 'like tutorial?']
['i', 'like tutorial?']
n, r, and rn can all be used as line breaks.
numpy.char.strip()
The numpy.char.strip() function is used to remove specific characters from the beginning or end.
Example
import numpy as np
print(np.char.strip('ashok atutoriala', 'a'))
print(np.char.strip(['atutoriala', 'admin', 'java'], 'a'))
The output is:
shok atutorial
['tutorial' 'dmin' 'jav']
numpy.char.join()
The numpy.char.join() function joins elements or strings in an array by a specified delimiter.
Example
import numpy as np
print(np.char.join(':', 'tutorial'))
print(np.char.join([':', '-'], ['tutorial', 'google']))
The output is:
r:u:n:o:o:b
['r:u:n:o:o:b' 'g-o-o-g-l-e']
numpy.char.replace()
The numpy.char.replace() function replaces all occurrences of a substring in a string with a new string.
Example
import numpy as np
print(np.char.replace('i like tutorial', 'oo', 'cc'))
The output is:
i like runccb
numpy.char.encode()
The numpy.char.encode() function calls str.encode on each element in the array. The default encoding is utf-8, and standard Python library codecs can be used.
Example
import numpy as np
a = np.char.encode('tutorial', 'cp500')
print(a)
The output is:
b'x99xa4x95x96x96x82'
numpy.char.decode()
The numpy.char.decode() function decodes encoded elements using str.decode().
Example
import numpy as np
a = np.char.encode('tutorial', 'cp500')
print(a)
print(np.char.decode(a, 'cp500'))
The output is:
b'x99xa4x95x96x96x82'
tutorial
YouTip