What is the numpy.char.add() function from NumPy in Python?

Overview

The char.add() function in Python is simply used to return an element-wise string concatenation of two arrays, str, or Unicode.

Syntax

char.add(x1, x2)

Parameter value

The char.add() function takes the following mandatory parameter values.

  • x1: This represents the input array.
  • x2: This represents the second input array to be concatenated with the first input array.

Return value

The char.add() function returns an array of strins or unicodes.

Code

import numpy as np
# creatin the arrays
x1 = ['Hi', 'Welcome', 'board']
x2 = ['Pals', 'on', '!']
# implementing the char.add() method
myarray = np.char.add(x1, x2)
print(myarray)

Explanation

  • Line 1: We import the numpy module.
  • Lines 4-5: We create array variables x1 and x2.
  • Line 8: We use the char.add() function to concatenate the arrays, x1 and x2. The result is assigned to a variable myarray.
  • Line 8: We print the variable myarray.

Free Resources