Please note, this is a STATIC archive of website www.w3resource.com from 19 Jul 2022, cach3.com does not collect or store any user information, there is no "phishing" involved.
w3resource

NumPy: strip() function

numpy.core.defchararray.strip() function

For each element in a given array the numpy.core.defchararray.strip() function returns a copy with the leading and trailing characters removed.
Calls str.strip element-wise.

Version: 1.15.0

Syntax:

numpy.core.defchararray.strip(a, chars=None)

Parameter:

Name Description Required /
Optional
a: array-like of str or unicode Input an array_like of string or unicode. Required
chars: str or unicode The chars argument is a string specifying the set of characters to be removed. If omitted or None, the chars argument defaults to removing whitespace. The chars argument is not a prefix or suffix; rather, all combinations of its values are stripped. Optional

Return value:

out [ndarray] Output array of str or unicode, depending on input type.

Note:

The 'chararray' class exists for backwards compatibility with Numarray, it is not recommended for new development. Starting from numpy 1.4, if one needs arrays of strings, it is recommended to use arrays of 'dtype' 'object_', 'string_' or 'unicode_', and use the free functions in the 'numpy.char' module for fast vectorized string operations.

Some methods will only be available if the corresponding string method is available in your version of Python.

The preferred alias for 'defchararray' is 'numpy.char'.

Example-1: numpy.strip() function

>>> import numpy as np
>>> x = np.array(['aAaAaA', '  aA  ', 'abBABba'])
>>> x
array(['aAaAaA', '  aA  ', 'abBABba'], dtype='<U7')
>>> np.char.strip(x)
array(['aAaAaA', 'aA', 'abBABba'], dtype='<U7')
>>> np.char.strip(x, 'a')
array(['AaAaA', '  aA  ', 'bBABb'], dtype='<U7')
>>> np.char.strip(x, 'A')
array(['aAaAa', '  aA  ', 'abBABba'], dtype='<U7')

Example-2: numpy.strip() function

>>> import numpy as np
>>> a = np.char.strip('apple basket', 'a')
>>> a
array('pple basket', dtype='<U12')

Pictorial Presentation:

NumPy String operation: strip() function

Example-3: numpy.strip() function

>>> import numpy as np
>>> a = np.char.strip('basket ball', 'l')
>>> a
array('basket ba', dtype='<U11')

Pictorial Presentation:

NumPy String operation: strip() function

Python - NumPy Code Editor:

Previous: splitlines()
Next: swapcase()