w3resource
NumPy String Exercises

NumPy: Concatenate element-wise two arrays of string

NumPy String: Exercise-1 with Solution

Write a NumPy program to concatenate element-wise two arrays of string.

Sample Solution:-

Python Code:

import numpy as np
x1 = np.array(['Python', 'PHP'], dtype=np.str)
x2 = np.array([' Java', ' C++'], dtype=np.str)
print("Array1:")
print(x1)
print("Array2:")
print(x2)
new_array = np.char.add(x1, x2)
print("new array:")
print(new_array)

Sample Input:

(['Python', 'PHP'], dtype=np.str)
([' Java', ' C++'], dtype=np.str)

Sample Output:

Array1:
['Python' 'PHP']
Array2:
[' Java' ' C++']
new array:
['Python Java' 'PHP C++']

Pictorial Presentation:

NumPy String: Concatenate element-wise two arrays of string

Python Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: NumPy String Exercises Home.
Next: Write a NumPy program to repeat all the elements three times of a given array of string.

What is the difficulty level of this exercise?