Q:

Write a NumPy program to replace a specific character with another in a given array of string values

0

Write a NumPy program to replace a specific character with another in a given array of string values.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

import numpy as np 

str1 = np.array([['Python-NumPy-Exercises'],
              ['-Python-']])
print("Original array of string values:") 
print(str1)
print("\nReplace '-' with '=' character in the said array of string values:")
print(np.char.strip(np.char.replace(str1, '-', '==')))
print("\nReplace '-' with ' ' character in the said array of string values:")
print(np.char.strip(np.char.replace(str1, '-', ' ')))

Sample Output:

Original array of string values:
[['Python-NumPy-Exercises']
 ['-Python-']]

Replace '-' with '=' character in the said array of string values:
[['Python==NumPy==Exercises']
 ['==Python==']]

Replace '-' with ' ' character in the said array of string values:
[['Python NumPy Exercises']
 ['Python']]

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now