numpy.char.add() function in Python
The add() method of the char class in the NumPy module is used for element-wise string concatenation for two arrays of str or unicode.
numpy.char.add()
Syntax : numpy.char.add(x1, x2)
Parameters :
- x1 : first array to be concatenated (concatenated at the beginning)
- x2 : second array to be concatenated (concatenated at the end)
Returns : Array of strings or unicode
Example 1 : Using the method on 2 single element string arrays.
Python3
import numpy as np
x1 = [ 'Hello' ]
x2 = [ 'World' ]
print ( "The arrays are : " )
print (x1)
print (x2)
result = np.char.add(x1, x2)
print ( "\nThe concatenated array is :" )
print (result)
|
Output :
The arrays are :
['Hello']
['World']
The concatenated array is :
['HelloWorld']
Example 2 : Using the method on 2 multiple elements string arrays.
Python3
import numpy as np
x1 = [ 'Hello' , 'to' , 'all' ]
x2 = [ 'Geeks' , 'for' , 'Geeks' ]
print ( "The arrays are : " )
print (x1)
print (x2)
result = np.char.add(x1, x2)
print ( "\nThe concatenated array is :" )
print (result)
|
Output :
The arrays are :
['Hello', 'to', 'all']
['Geeks', 'for', 'Geeks']
The concatenated array is :
['HelloGeeks' 'tofor' 'allGeeks']
Last Updated :
29 Aug, 2020
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...