numpy.char.replace#
- char.replace(a, old, new, count=-1)[source]#
For each element in
a, return a copy of the string with occurrences of substringoldreplaced bynew.- Parameters:
- aarray_like, with
bytes_orstr_dtype - old, newarray_like, with
bytes_orstr_dtype - countarray_like, with
int_dtype If the optional argument
countis given, only the firstcountoccurrences are replaced.
- aarray_like, with
- Returns:
- outndarray
Output array of
StringDType,bytes_orstr_dtype, depending on input types
See also
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')