- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Return a copy of the string with all occurrences of substring old replaced by new in Numpy
To return a copy of the string with all occurrences of substring old replaced by new, use the numpy.char.replace() method in Python Numpy −
- The 1st parameter is the input array
- The 2nd parameter is the old string to be replaced
- The 3rd parameter is the new string to be replaced with the old
If the optional parameter count is given, only the first count occurrences are replaced
The numpy.char module provides a set of vectorized string operations for arrays of type numpy.str_ or numpy.bytes_.
Steps
At first, import the required library −
import numpy as np
Create an array −
arr = np.array(["Welcome to the Jungle", "Jungle Safari"])
Displaying our array −
print("Array...
",arr)
Get the datatype −
print("
Array datatype...
",arr.dtype)
Get the dimensions of the Array −
print("
Array Dimensions...
",arr.ndim)
Get the shape of the Array −
print("
Our Array Shape...
",arr.shape)
Get the number of elements of the Array −
print("
Elements in the Array...
",arr.size)
To return a copy of the string with all occurrences of substring old replaced by new, use the numpy.char.replace() method in Python Numpy −
print("
Result...
",np.char.replace(arr, 'Jungle', 'Club'))
Example
import numpy as np # Create an array arr = np.array(["Welcome to the Jungle", "Jungle Safari"]) # Displaying our Array print("Array...
",arr) # Get the datatype print("
Array datatype...
",arr.dtype) # Get the dimensions of the Array print("
Array Dimensions...
",arr.ndim) # Get the shape of the Array print("
Our Array Shape...
",arr.shape) # Get the number of elements of the Array print("
Elements in the Array...
",arr.size) # To return a copy of the string with all occurrences of substring old replaced by new, use the numpy.char.replace() method in Python Numpy # The 1st parameter is the input array # The 2nd parameter is the old string to be replaced # The 3rd parameter is the new string to be replaced with the old print("
Result...
",np.char.replace(arr, 'Jungle', 'Club'))
Output
Array... ['Welcome to the Jungle' 'Jungle Safari'] Array datatype... <U21 Array Dimensions... 1 Our Array Shape... (2,) Elements in the Array... 2 Result... ['Welcome to the Club' 'Club Safari']
- Related Articles
- Return a copy of each string element where all tab characters are replaced by spaces in Numpy
- Replace All Occurrences of a Python Substring with a New String?
- How can we replace all the occurrences of a substring with another substring within a string in MySQL?
- Copy and return all the elements of a masked array in Numpy
- Return an array with the number of nonoverlapping occurrences of substring in Python
- Java Program to replace all occurrences of given String with new one
- Return input with invalid data masked and replaced by a fill value in Numpy
- Return a copy of the masked array in NumPy
- Return a copy of an array with its elements centered in a string of length width in Numpy
- Python – All occurrences of Substring from the list of strings
- Return element-wise a copy of the string with uppercase characters converted to lowercase and vice versa in Numpy
- Return a copy of self, with masked values filled with a given value in Numpy
- Python program – All occurrences of Substring from the list of strings
- How to replace all occurrences of a string with another string in Python?
- Return a new array of given shape filled with ones in Numpy

Advertisements