Python numpy.char.add() Method (With Examples)

By IncludeHelp Last updated : November 28, 2023

Python numpy.char.add() Method

The add() method is a built-in method of char class in the numpy module, it is used to concatenate two arrays of string or Unicode element-wise.

Module

The following module is required to use the add() method:

import numpy as np

Syntax

The following is the syntax of the add() method:

char.add(arr1, arr2)

Parameter(s)

The following are the parameter(s):

  • arr1: The first array-like value which can be string, array of string, or Unicode.
  • arr2: The second array-like value which can be string, array of string, or Unicode.

Return Value

The return type of add() method is <class 'numpy.ndarray'>, it returns element-wise concatenated array of bytes_ or str_.

Example 1: Using single element strings

# Importing numpy module
import numpy as np

# Creating two arrays with
# single element string
arr1 = ["Include"]
arr2 = ["Help"]

# Printing the arrays
print("arr1: ", arr1)
print("arr2: ", arr2)

# Using char.add() method
result = np.char.add(arr1, arr2)
print("The concatenated array is (result):", result)
print("The type of the result is:", type(result))

Output

The output of the above example is:

arr1:  ['Include']
arr2:  ['Help']
The concatenated array is (result): ['IncludeHelp']
The type of the result is: <class 'numpy.ndarray'>

Example 2: Using array of strings

# Importing numpy module
import numpy as np

# Creating two arrays with
# arrays of strings
arr1 = ["Hello", "Hi", "Hey", "Include"]
arr2 = ["World", "Earth", "Sun", "Help"]

# Printing the arrays
print("arr1: ", arr1)
print("arr2: ", arr2)

# Using char.add() method
result = np.char.add(arr1, arr2)
print("\nThe concatenated array is (result):\n", result)
print("\nThe type of the result is:", type(result))

Output

The output of the above example is:

arr1:  ['Hello', 'Hi', 'Hey', 'Include']
arr2:  ['World', 'Earth', 'Sun', 'Help']

The concatenated array is (result):
 ['HelloWorld' 'HiEarth' 'HeySun' 'IncludeHelp']

The type of the result is: <class 'numpy.ndarray'>

Python NumPy Programs »

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.