Skip to content
Related Articles

Related Articles

Improve Article
numpy.char.add() function in Python
  • 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 :

  • 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 = ['Geeks', 'for', 'Geeks']
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']
['Geeks', 'for', 'Geeks']

The concatenated array is :
['HelloGeeks' 'tofor' 'allGeeks']


 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.  

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course




My Personal Notes arrow_drop_up
Recommended Articles
Page :