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 standard string functions from the Python built-in library.
These functions are defined in the character array class (numpy.char
).
Function | Description |
---|---|
add() | Concatenates corresponding elements of two arrays |
multiply() | Returns element-wise string multiple concatenation |
center() | Centers the string |
capitalize() | Converts the first character of the string to uppercase |
title() | Converts the first character of each word to uppercase |
lower() | Converts array elements to lowercase |
upper() | Converts array elements to uppercase |
split() | Splits the string at the specified separator and returns an array of strings |
splitlines() | Returns a list of lines in the string, breaking at line boundaries |
strip() | Removes specified characters from the beginning and end of the element |
join() | Joins the elements of an array with the specified separator |
replace() | Replaces all occurrences of a substring with a new string |
decode() | Calls str.decode on each element |
encode() | Calls str.encode on each element |
numpy.char.add()
The numpy.char.add()
function concatenates elements of two arrays sequentially.
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']))
Output:
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('tutorialpro ', 3))
Output:
tutorialpro tutorialpro tutorialpro
numpy.char.center()
The numpy.char.center()
function centers the string and pads it with specified characters on the left and right.
Example
import numpy as np
# np.char.center(str, width, fillchar)
# str: string, width: length, fillchar: padding character
print(np.char.center('tutorialpro', 20, fillchar='*'))
Output:
*******tutorialpro*******
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('tutorialpro'))
Output:
tutorialpro
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 tutorialpro'))
Output:
I Like tutorialpro
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
# Operating on array
print(np.char.lower(['tutorialpro', 'GOOGLE']))
# Operating on string
print(np.char.lower('tutorialpro'))
Output:
['tutorialpro' 'google']
tutorialpro
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
# Operating on array
print(np.char.upper(['tutorialpro', 'google']))
Output:
['TUTORIALPRO' 'GOOGLE']
print(np.char.upper(['tutorialpro', 'google']))
# Operating on strings
print(np.char.upper('tutorialpro'))
Output result:
['TUTORIALPRO' 'GOOGLE'] TUTORIALPRO
### numpy.char.split()
numpy.char.split() splits the string by the specified delimiter and returns an array. By default, the delimiter is a space.
## Example
```python
import numpy as np
# Default delimiter is a space
print(np.char.split('i like tutorialpro?'))
# Delimiter is .
print(np.char.split('www.tutorialpro.org', sep='.'))
Output result:
['i', 'like', 'tutorialpro?']
['www', 'tutorialpro', 'org']
numpy.char.splitlines()
numpy.char.splitlines() splits the string by newline characters and returns an array.
Example
import numpy as np
# Newline character \n
print(np.char.splitlines('i\nlike tutorialpro?'))
print(np.char.splitlines('i\rlike tutorialpro?'))
Output result:
['i', 'like tutorialpro?']
['i', 'like tutorialpro?']
\n
, \r
, \r\n
can all be used as newline characters.
numpy.char.strip()
numpy.char.strip() removes specific characters from the beginning or end.
Example
import numpy as np
# Remove 'a' characters from the beginning and end of the string
print(np.char.strip('ashok tutorialproa', 'a'))
# Remove 'a' characters from the beginning and end of array elements
print(np.char.strip(['atutorialproa', 'admin', 'java'], 'a'))
Output result:
shok tutorialpro
['tutorialpro' 'dmin' 'jav']
numpy.char.join()
numpy.char.join() joins elements or strings in an array with a specified delimiter.
Example
import numpy as np
# Operate on strings
print(np.char.join(':', 'tutorialpro'))
# Specify multiple delimiters for array elements
print(np.char.join([':', '-'], ['tutorialpro', 'google']))
Output result:
t:u:t:o:r:i:a:l:p:r:o
['t:u:t:o:r:i:a:l:p:r:o' 'g-o-o-g-l-e']
numpy.char.replace()
numpy.char.replace() replaces all occurrences of a substring with a new string.
Example
import numpy as np
print(np.char.replace('i like tutorialpro', 'oo', 'cc'))
Output result:
i like tutilcpro
numpy.char.encode()
numpy.char.encode() calls str.encode on each element of the array. The default encoding is utf-8, and standard Python codecs can be used.
Example
import numpy as np
a = np.char.encode('tutorialpro', 'cp500')
print(a)
Output result:
b'\x99\xa4\x95\x96\x96\x82'
numpy.char.decode()
numpy.char.decode() decodes the encoded elements using str.decode().
Example
import numpy as np
a = np.char.encode('tutorialpro', 'cp500')
print(a)
print(np.char.decode(a, 'cp500'))
Output result:
b'\x99\xa4\x95\x96\x96\x82'
tutorialpro