NumPy String Functions
The following functions are used to perform vectorized string operations on arrays with dtype 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 the string elements of two arrays element-wise. |
| multiply() | Returns a string after element-wise multiple concatenation. |
center() |
Centers a string |
capitalize() |
Converts the first letter of a string to uppercase. |
title() |
Converts the first letter of each word in a string to uppercase. |
lower() |
Converts array elements to lowercase. |
upper() |
Converts array elements to uppercase. |
split() |
Splits strings by a specified delimiter and returns an array list. |
splitlines() |
Returns a list of lines in elements, split by newline characters. |
strip() |
Removes specific characters from the beginning or end of elements. |
join() |
Joins elements in an array by a specified delimiter. |
replace() |
Replaces all substrings in a string with a new string. |
decode() |
Calls on array elements in sequence.str.decode |
encode() |
Calls on array elements in sequence.str.encode |
numpy.char.add()
The numpy.char.add() function performs string concatenation on the elements of two arrays element-wise.
Example
The output result is:
连接两个字符串: ['hello xyz'] 连接示例: ['hello abc' 'hi xyz']
numpy.char.multiply()
The numpy.char.multiply() function performs multiple concatenation.
Example
The output result is:
Example Example Example
numpy.char.center()
The numpy.char.center() function is used to center strings and pad them with a specified character on the left and right.
Example
The output result is:
*******Example*******
numpy.char.capitalize()
The numpy.char.capitalize() function converts the first letter of a string to uppercase:
Example
The output result is:
Example
numpy.char.title()
The numpy.char.title() function converts the first letter of each word in a string to uppercase:
Example
The output result is:
I Like Example
numpy.char.lower()
The numpy.char.lower() function converts each element of an array to lowercase. It calls str.lower on each element.
Example
The output result is:
['example' 'google'] example
numpy.char.upper()
The numpy.char.upper() function converts each element of an array to uppercase. It calls str.upper on each element.
Example
The output result is:
['EXAMPLE' 'GOOGLE'] EXAMPLE
numpy.char.split()
numpy.char.split() splits strings by a specified delimiter and returns an array. By default, the delimiter is a space.
Example
The output result is:
['i', 'like', 'example?'] ['www', 'example', 'com']
numpy.char.splitlines()
The numpy.char.splitlines() function splits strings using newline characters as delimiters and returns an array.
Example
The output result is:
['i', 'like example?'] ['i', 'like example?']
\n,\r,\r\nAll can be used as newline characters.
numpy.char.strip()
The numpy.char.strip() function is used to remove specific characters from the beginning or end.
Example
The output result is:
shok aexample ['example' 'dmin' 'jav']
numpy.char.join()
The numpy.char.join() function joins elements in an array or strings by a specified delimiter.
Example
The output result 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 substrings in a string with a new string.
Example
The output result is:
i like runccb
numpy.char.encode()
The numpy.char.encode() function calls the str.encode function on each element in the array. The default encoding is utf-8, and codecs from the standard Python library can be used.
Example
The output result is:
b'\x99\xa4\x95\x96\x96\x82'
numpy.char.decode()
The numpy.char.decode() function decodes encoded elements using str.decode().
Example
The output result is:
b'\x99\xa4\x95\x96\x96\x82' exampleOther Extensions