Element-wise concatenation of two NumPy arrays of string

In this article, we will discuss how to concatenate element-wise two arrays of string 

Example :

Input :
A = ['Akash', 'Ayush', 'Diksha', 'Radhika']
B = ['Kumar', 'Sharma', 'Tewari', 'Pegowal']

Output :
A + B = [AkashKumar, AyushSharma, 'DikshTewari', 'RadhikaPegowal']

We will be using the numpy.char.add() method.

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: String array with a single element.

Python3



filter_none

edit
close

play_arrow

link
brightness_4
code

# importing library numpy as np
import numpy as np
  
# creating array as first_name
first_name = np.array(['Geeks'], 
                      dtype = np.str)
print("Printing first name array:")
print(first_name)
  
# creating array as last name
last_name = np.array(['forGeeks'], 
                     dtype = np.str)
print("Printing last name array:")
print(last_name)
  
# concate first_name and last_name 
# into new array named as full_name
full_name = np.char.add(first_name, last_name)
print("\nPrinting concatenate array as full name:")
print(full_name)

chevron_right


Output : 

Printing first name array:
['Geeks']
Printing last name array:
['forGeeks']

Printing concatenate array as full name:
['GeeksforGeeks']

Example 2: String array with multiple elements.

Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# importing library numpy as np
import numpy as np
  
# creating array as first_name
first_name = np.array(['Akash', 'Ayush', 'Diksha'
                       'Radhika'], dtype = np.str)
print("Printing first name array:")
print(first_name)
  
# creating array as last name
last_name = np.array(['Kumar', 'Sharma', 'Tewari'
                      'Pegowal'], dtype = np.str)
print("Printing last name array:")
print(last_name)
  
# concate first_name and last_name 
# into new array named as full_name
full_name = np.char.add(first_name, last_name)
print("\nPrinting concatenate array as full name:")
print(full_name)

chevron_right


Output : 

Printing first name array:
['Akash' 'Ayush' 'Diksha' 'Radhika']
Printing last name array:
['Kumar' 'Sharma' 'Tewari' 'Pegowal']

Printing concatenate array as full name:
['AkashKumar' 'AyushSharma' 'DikshaTewari' 'RadhikaPegowal']



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.