numpy.char.add() function in Python (original) (raw)

Last Updated : 29 Aug, 2020

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 :

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']