String operations

The numpy.char module provides a set of vectorized string operations for arrays of type numpy.string_ or numpy.unicode_. All of them are based on the string methods in the Python standard library.

String operations

methoddescription
addopen in new window(x1, x2)Return element-wise string concatenation for two arrays of str or unicode.
multiplyopen in new window(a, i)Return (a * i), that is string multiple concatenation, element-wise.
modopen in new window(a, values)Return (a % i), that is pre-Python 2.6 string formatting (iterpolation), element-wise for a pair of array_likes of str or unicode.
capitalizeopen in new window(a)Return a copy of a with only the first character of each element capitalized.
centeropen in new window(a, width[, fillchar])Return a copy of a with its elements centered in a string of length width.
decodeopen in new window(a[, encoding, errors])Calls str.decode element-wise.
encodeopen in new window(a[, encoding, errors])Calls str.encode element-wise.
expandtabsopen in new window(a[, tabsize])Return a copy of each string element where all tab characters are replaceopen in new windowd by one or more spaces.
joinopen in new window(sep, seq)Return a string which is the concatenation of the strings in the sequence seq.
ljustopen in new window(a, width[, fillchar])Return an array with the elements of a left-justified in a string of length width.
loweropen in new window(a)Return an array with the elements converted to lowercase.
lstripopen in new window(a[, chars])For each element in a, return a copy with the leading characters removed.
partitionopen in new window(a, sep)Partition each element in a around sep.
replaceopen in new window(a, old, new[, count])For each element in a, return a copy of the string with all occurrences of substring old replaced by new.
rjustopen in new window(a, width[, fillchar])Return an array with the elements of a right-justified in a string of length width.
rpartitionopen in new window(a, sep)Partition (splitopen in new window) each element around the right-most separator.
rsplitopen in new window(a[, sep, maxsplit])For each element in a, return a list of the words in the string, using sep as the delimiter string.
rstripopen in new window(a[, chars])For each element in a, return a copy with the trailing characters removed.
splitopen in new window(a[, sep, maxsplit])For each element in a, return a list of the words in the string, using sep as the delimiter string.
splitlinesopen in new window(a[, keepends])For each element in a, return a list of the lines in the element, breaking at line boundaries.
stripopen in new window(a[, chars])For each element in a, return a copy with the leading and trailing characters removed.
swapcaseopen in new window(a)Return element-wise a copy of the string with upperopen in new windowcase characters converted to lowercase and vice versa.
titleopen in new window(a)Return element-wise title cased version of string or unicode.
translateopen in new window(a, table[, deletechars])For each element in a, return a copy of the string where all characters occurring in the optional argument deletechars are removed, and the remaining characters have been mapped through the given translation table.
upperopen in new window(a)Return an array with the elements converted to uppercase.
zfillopen in new window(a, width)Return the numeric string left-filled with zeros

Comparison

Unlike the standard numpy comparison operators, the ones in the char module strip trailing whitespace characters before performing the comparison.

methoddescription
equalopen in new window(x1, x2)Return (x1 == x2) element-wise.
not_equalopen in new window(x1, x2)Return (x1 != x2) element-wise.
greater_equalopen in new window(x1, x2)Return (x1 >= x2) element-wise.
less_equalopen in new window(x1, x2)Return (x1 <= x2) element-wise.
greateropen in new window(x1, x2)Return (x1 > x2) element-wise.
lessopen in new window(x1, x2)Return (x1 < x2) element-wise.
compare_chararraysopen in new window(a, b, cmp_op, rstrip)Performs element-wise comparison of two string arrays using the comparison operator specified by cmp_op.

String information

methoddescription
countopen in new window(a, sub[, start, end])Returns an array with the number of non-overlapping occurrences of substring sub in the range [start, end].
endswithopen in new window(a, suffix[, start, end])Returns a boolean array which is True where the string element in a ends with suffix, otherwise False.
findopen in new window(a, sub[, start, end])For each element, return the lowest indexopen in new window in the string where substring sub is found.
indexopen in new window(a, sub[, start, end])Like find, but raises ValueError when the substring is not found.
isalphaopen in new window(a)Returns true for each element if all characters in the string are alphabetic and there is at least one character, false otherwise.
isalnumopen in new window(a)Returns true for each element if all characters in the string are alphanumeric and there is at least one character, false otherwise.
isdecimalopen in new window(a)For each element, return True if there are only decimal characters in the element.
isdigitopen in new window(a)Returns true for each element if all characters in the string are digits and there is at least one character, false otherwise.
isloweropen in new window(a)Returns true for each element if all cased characters in the string are lowercase and there is at least one cased character, false otherwise.
isnumericopen in new window(a)For each element, return True if there are only numeric characters in the element.
isspaceopen in new window(a)Returns true for each element if there are only whitespace characters in the string and there is at least one character, false otherwise.
istitleopen in new window(a)Returns true for each element if the element is a titlecased string and there is at least one character, false otherwise.
isupperopen in new window(a)Returns true for each element if all cased characters in the string are uppercase and there is at least one character, false otherwise.
rfindopen in new window(a, sub[, start, end])For each element in a, return the highest index in the string where substring sub is found, such that sub is contained within [start, end].
rindexopen in new window(a, sub[, start, end])Like rfind, but raises ValueError when the substring sub is not found.
startswithopen in new window(a, prefix[, start, end])Returns a boolean array which is True where the string element in a starts with prefix, otherwise False.
str_lenopen in new window(a)Return len(a) element-wise.

Convenience class

methoddescription
arrayopen in new window(obj[, itemsize, copy, unicode, order])Create a chararrayopen in new window.
asarrayopen in new window(obj[, itemsize, unicode, order])Convert the input to a chararrayopen in new window, copying the data only if necessary.
chararrayopen in new window(shape[, itemsize, unicode, …])Provides a convenient view on arrays of string and unicode values.