numpy.char.array — NumPy v2.6.dev0 Manual

char.array(obj, itemsize=None, copy=True, unicode=None, order=None)

[source]

#

Create a

chararray

.

Deprecated since version 2.5: chararray is deprecated. Use an ndarray with a string or bytes dtype instead.

Note

This class is provided for numarray backward-compatibility. New code (not concerned with numarray compatibility) should use arrays of type

bytes_

or

str_

and use the free functions in

numpy.char

for fast vectorized string operations instead.

Versus a NumPy array of dtype

bytes_

or

str_

, this class adds the following functionality:

values automatically have whitespace removed from the end when indexed

comparison operators automatically remove whitespace from the end when comparing values

vectorized string operations are provided as methods (e.g.

chararray.endswith

) and infix operators (e.g. +,*,%)

Parameters:objarray of str or unicode-likeitemsizeint, optionalitemsize is the number of characters per scalar in the resulting array. If itemsize is None, and obj is an object array or a Python list, the itemsize will be automatically determined. If itemsize is provided and obj is of type str or unicode, then the obj string will be chunked into itemsize pieces.

copybool, optionalIf true (default), then the object is copied. Otherwise, a copy will only be made if __array__ returns a copy, if obj is a nested sequence, or if a copy is needed to satisfy any of the other requirements (itemsize, unicode, order, etc.).

unicodebool, optionalWhen true, the resulting

chararray

can contain Unicode characters, when false only 8-bit characters. If unicode is None and obj is one of the following:

a

chararray

,

an ndarray of type

str_

or

bytes_

a Python

str

or

bytes

object,

then the unicode setting of the output array will be automatically determined.

order{‘C’, ‘F’, ‘A’}, optionalSpecify the order of the array. If order is ‘C’ (default), then the array will be in C-contiguous order (last-index varies the fastest). If order is ‘F’, then the returned array will be in Fortran-contiguous order (first-index varies the fastest). If order is ‘A’, then the returned array may be in any order (either C-, Fortran-contiguous, or even discontiguous).

Examples

>>> importnumpyasnp>>> char_array=np.char.array(['hello','world','numpy','array'])>>> char_arraychararray(['hello', 'world', 'numpy', 'array'], dtype='<U5')