numpy.strings.strip — NumPy v2.6.dev0 Manual

strings.strip(a, chars=None)

[source]

#

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

Parameters:aarray-like, with StringDType, bytes_, or str_ dtypecharsscalar with the same dtype as a, optionalThe chars argument is a string specifying the set of characters to be removed. If 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.

Returns:outndarrayOutput array of StringDType, bytes_ or str_ dtype, depending on input types

Examples

>>> importnumpyasnp>>> c=np.array(['aAaAaA',' aA ','abBABba'])>>> carray(['aAaAaA', ' aA ', 'abBABba'], dtype='<U7')>>> np.strings.strip(c)array(['aAaAaA', 'aA', 'abBABba'], dtype='<U7')# 'a' unstripped from c[1] because of leading whitespace.>>> np.strings.strip(c,'a')array(['AaAaA', ' aA ', 'bBABb'], dtype='<U7')# 'A' unstripped from c[1] because of trailing whitespace.>>> np.strings.strip(c,'A')array(['aAaAa', ' aA ', 'abBABba'], dtype='<U7')