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

import numpy as np print ('Concatenate two strings:') print (np.char.add(['hello'],[' xyz'])) print ('\n') print ('Concatenation example:') print (np.char.add(['hello', 'hi'],[' abc', ' xyz']))

The output result is:

连接两个字符串:
['hello xyz']

连接示例:
['hello abc' 'hi xyz']

numpy.char.multiply()

The numpy.char.multiply() function performs multiple concatenation.

Example

import numpy as np print (np.char.multiply('Example ',3))

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

import numpy as np # np.char.center(str , width,fillchar) : # str: string, width: length, fillchar: padding character print (np.char.center('Example', 20,fillchar = '*'))

The output result is:

*******Example*******

numpy.char.capitalize()

The numpy.char.capitalize() function converts the first letter of a string to uppercase:

Example

import numpy as np print (np.char.capitalize('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

import numpy as np print (np.char.title('i like 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

import numpy as np # Operate on array print (np.char.lower(['EXAMPLE','GOOGLE'])) # Operate on string print (np.char.lower('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

import numpy as np # Operate on array print (np.char.upper(['example','google'])) # Operate on string print (np.char.upper('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

import numpy as np # delimiter defaults to space print (np.char.split ('i like example?')) # delimiter is . print (np.char.split ('www.example.com', sep = '.'))

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

import numpy as np # newline character \n print (np.char.splitlines('i\nlike example?')) print (np.char.splitlines('i\rlike 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

import numpy as np # Remove the character a from the beginning and end of the string print (np.char.strip('ashok aexamplea','a')) # Remove the character a from the beginning and end of array elements print (np.char.strip(['aexamplea','admin','java'],'a'))

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

import numpy as np # Operate on string print (np.char.join(':','example')) # Specify multiple delimiters to operate on array elements print (np.char.join([':','-'],['example','google']))

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

import numpy as np print (np.char.replace ('i like example', 'oo', 'cc'))

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

import numpy as np a = np.char.encode('example', 'cp500') print (a)

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

import numpy as np a = np.char.encode('example', 'cp500') print (a) print (np.char.decode(a,'cp500'))

The output result is:

b'\x99\xa4\x95\x96\x96\x82'
example
Other Extensions