Please note, this is a STATIC archive of website www.w3resource.com from 19 Jul 2022, cach3.com does not collect or store any user information, there is no "phishing" involved.
w3resource

NumPy: add() function

numpy.core.defchararray.add() function

The numpy.core.defchararray.add() function is used to create element-wise string concatenation for two given arrays of str or unicode.
The two given arrays must have the same shape.

Version: 1.15.0

Syntax:

numpy.core.defchararray.add(x1, x2)

Parameter:

Name Description Required /
Optional
x1: array_like of str or unicode Input array. Required
x2: array_like of str or unicode Input array Required

Return value:

add [ndarray] Output array of string_ or unicode_, depending on input types of the same shape as x1 and x2.

Note:

The 'chararray' class exists for backwards compatibility with Numarray, it is not recommended for new development. Starting from numpy 1.4, if one needs arrays of strings, it is recommended to use arrays of 'dtype' 'object_', 'string_' or 'unicode_', and use the free functions in the 'numpy.char' module for fast vectorized string operations.

Some methods will only be available if the corresponding string method is available in your version of Python.

The preferred alias for 'defchararray' is 'numpy.char'.

Example-1: numpy.char.add() function

>>> import numpy as np
>>> x1 = np.array(['p', 't'])
>>> x2 = np.array(['Y', 'H'])
>>> np.char.add(x1, x2)
array(['pY', 'tH'], dtype='<U2')

Pictorial Presentation:

NumPy String operation: add() function

Example-2: numpy.char.add() function

>> import numpy as np
>>> s1 = np.array(("Java"))
>>> s2 = np.array(("Script"))
>>> np.char.add(s1, s2)
array('JavaScript', dtype='<U10')

Pictorial Presentation:

NumPy String operation: add() function

Python - NumPy Code Editor:

Previous: NumPy String operation Home
Next: multiply()