numpy.char.array#

char.array(obj,itemsize=None,copy=True,unicode=None,order=None)[source]#

Create achararray.

Note

This class is provided for numarray backward-compatibility.New code (not concerned with numarray compatibility) should usearrays of typebytes_ orstr_ and use the free functionsinnumpy.char for fast vectorized string operations instead.

Versus a NumPy array of dtypebytes_ orstr_, thisclass adds the following functionality:

  1. values automatically have whitespace removed from the endwhen indexed

  2. comparison operators automatically remove whitespace from theend when comparing values

  3. vectorized string operations are provided as methods(e.g.chararray.endswith)and infix operators (e.g.+,*,%)

Parameters:
objarray of str or unicode-like
itemsizeint, optional

itemsize is the number of characters per scalar in theresulting array. Ifitemsize is None, andobj is anobject array or a Python list, theitemsize will beautomatically determined. Ifitemsize is provided andobjis of type str or unicode, then theobj string will bechunked intoitemsize pieces.

copybool, optional

If true (default), then the object is copied. Otherwise, a copywill only be made if__array__ returns a copy, if obj is anested sequence, or if a copy is needed to satisfy any of the otherrequirements (itemsize, unicode,order, etc.).

unicodebool, optional

When true, the resultingchararray can contain Unicodecharacters, when false only 8-bit characters. If unicode isNone andobj is one of the following:

then the unicode setting of the output array will beautomatically determined.

order{‘C’, ‘F’, ‘A’}, optional

Specify the order of the array. If order is ‘C’ (default), then thearray will be in C-contiguous order (last-index varies thefastest). If order is ‘F’, then the returned arraywill be in Fortran-contiguous order (first-index varies thefastest). If order is ‘A’, then the returned array maybe in any order (either C-, Fortran-contiguous, or evendiscontiguous).

Examples

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