String functionality#

The numpy.strings module provides a set of universal functions operating on arrays of type numpy.str_ or numpy.bytes_. For example

>>> np.strings.add(["num", "doc"], ["py", "umentation"])
array(['numpy', 'documentation'], dtype='<U13')

These universal functions are also used in numpy.char, which provides the numpy.char.chararray array subclass, in order for those routines to get the performance benefits as well.

Note

Prior to NumPy 2.0, all string functionality was in numpy.char, which only operated on fixed-width strings. That module will not be getting updates and will be deprecated at some point in the future.

String operations#

add(x1, x2, /[, out, where, casting, order, ...])

Add arguments element-wise.

lstrip(a[, chars])

For each element in a, return a copy with the leading characters removed.

rstrip(a[, chars])

For each element in a, return a copy with the trailing characters removed.

strip(a[, chars])

For each element in a, return a copy with the leading and trailing characters removed.

Comparison#

The numpy.strings module also exports the comparison universal functions that can now operate on string arrays as well.

equal(x1, x2, /[, out, where, casting, ...])

Return (x1 == x2) element-wise.

not_equal(x1, x2, /[, out, where, casting, ...])

Return (x1 != x2) element-wise.

greater_equal(x1, x2, /[, out, where, ...])

Return the truth value of (x1 >= x2) element-wise.

less_equal(x1, x2, /[, out, where, casting, ...])

Return the truth value of (x1 <= x2) element-wise.

greater(x1, x2, /[, out, where, casting, ...])

Return the truth value of (x1 > x2) element-wise.

less(x1, x2, /[, out, where, casting, ...])

Return the truth value of (x1 < x2) element-wise.

String information#

count(a, sub[, start, end])

Returns an array with the number of non-overlapping occurrences of substring sub in the range [start, end).

endswith(a, suffix[, start, end])

Returns a boolean array which is True where the string element in a ends with suffix, otherwise False.

find(a, sub[, start, end])

For each element, return the lowest index in the string where substring sub is found, such that sub is contained in the range [start, end).

isalpha(x, /[, out, where, casting, order, ...])

Returns true for each element if all characters in the data interpreted as a string are alphabetic and there is at least one character, false otherwise.

isdecimal(x, /[, out, where, casting, ...])

For each element, return True if there are only decimal characters in the element.

isdigit(x, /[, out, where, casting, order, ...])

Returns true for each element if all characters in the string are digits and there is at least one character, false otherwise.

isnumeric(x, /[, out, where, casting, ...])

For each element, return True if there are only numeric characters in the element.

isspace(x, /[, out, where, casting, order, ...])

Returns true for each element if there are only whitespace characters in the string and there is at least one character, false otherwise.

rfind(a, sub[, start, end])

For each element, return the highest index in the string where substring sub is found, such that sub is contained in the range [start, end).

startswith(a, prefix[, start, end])

Returns a boolean array which is True where the string element in a starts with prefix, otherwise False.

str_len(x, /[, out, where, casting, order, ...])

Returns the length of each element.