Element-wise concatenation of two NumPy arrays of string
Last Updated :
04 Jan, 2022
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
# 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)
# concat 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)
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
# 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)
# concat 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)
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']
Similar Reads
Find the length of each string element in the Numpy array NumPy builds on (and is a successor to) the successful Numeric array object. Its goal is to create the corner-stone for a useful environment for scientific computing. NumPy provides two fundamental objects: an N-dimensional array object (ndarray) and a universal function object (ufunc). In this post
3 min read
How to Concatenate two 2-dimensional NumPy Arrays? Sometimes it might be useful or required to concatenate or merge two or more of these NumPy arrays. In this article, we will discuss various methods of concatenating two 2D arrays. But first, we have to import the NumPy package to use it: # import numpy package import numpy as np Then two 2D arrays
4 min read
Evaluate Einstein's summation convention of two multidimensional NumPy arrays In Python, we can use the einsum() function of the NumPy package to compute Einstein's summation convention of two given multidimensional arrays. Syntax: numpy.einsum(subscripts, *operands, out=None)Â Parameters:Â subscripts : str Specifies the subscripts for summation as comma separated list of sub
3 min read
How to Concatenate Two Lists Index Wise in Python Concatenating two lists index-wise means combining corresponding elements from both lists into a single element, typically a string, at each index . For example, given two lists like a = ["gf", "i", "be"] and b = ["g", "s", "st"], the task is to concatenate each element from a with the corresponding
3 min read
How to get values of an NumPy array at certain index positions? Sometimes we need to remove values from the source Numpy array and add them at specific indices in the target array. In NumPy, we have this flexibility, we can remove values from one array and add them to another array. We can perform this operation using numpy.put() function and it can be applied t
4 min read