bundles / numpy 2.4.4 / numpy / strings / replace
_ArrayFunctionDispatcher
numpy.strings:replace
source: /numpy/_core/strings.py:1289
Signature
def replace(a, old, new, count = -1)Summary
For each element in a, return a copy of the string with occurrences of substring old replaced by new.
Parameters
a: array_like, with ``bytes_`` or ``str_`` dtypeold, new: array_like, with ``bytes_`` or ``str_`` dtypecount: array_like, with ``int_`` dtypeIf the optional argument
countis given, only the firstcountoccurrences are replaced.
Returns
out: ndarrayOutput array of
StringDType,bytes_orstr_dtype, depending on input types
Examples
import numpy as np a = np.array(["That is a mango", "Monkeys eat mangos"]) np.strings.replace(a, 'mango', 'banana')✓
array(['That is a banana', 'Monkeys eat bananas'], dtype='<U19')
a = np.array(["The dish is fresh", "This is it"]) np.strings.replace(a, 'is', 'was')✓
array(['The dwash was fresh', 'Thwas was it'], dtype='<U19')
See also
- str.replace
Aliases
numpy.char.replace