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




# importing the module
import numpy as np
  
# create the arrays
x1 = ['Hello']
x2 = ['World']
print("The arrays are : ")
print(x1)
print(x2)
  
# using the char.add() method
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




# importing the module
import numpy as np
  
# create the arrays
x1 = ['Hello', 'to', 'all']
x2 = ['Beginner', 'for', 'Beginner']
print("The arrays are : ")
print(x1)
print(x2)
  
# using the char.add() method
result = np.char.add(x1, x2)
print("\nThe concatenated array is :")
print(result)


Output :

The arrays are : 
['Hello', 'to', 'all']
['Beginner', 'for', 'Beginner']

The concatenated array is :
['HelloBeginner' 'tofor' 'allBeginner']




Contact Us