w3resource

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-Numpy 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?

Test your Programming skills with w3resource's quiz.



Share this Tutorial / Exercise on : Facebook and Twitter

Python: Tips of the Day

Debug With the Print() Function:

>>> for i in range(5):
...     print(i, end=', ' if i < 4 else '\n')
... 
0, 1, 2, 3, 4
>>> for i in range(5):
...     print(f'{i} & {i*i}', end=', ' if i < 4 else '\n')
... 
0 & 0, 1 & 1, 2 & 4, 3 & 9, 4 & 16