NumPy strings.add()
The numpy.strings.add() function performs element-wise string concatenation on input arrays. If the arrays have different shapes, they must be broadcastable to a common shape.
Syntax
</>
Copy
numpy.strings.add(x1, x2, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True)
Parameters
| Parameter | Type | Description |
|---|---|---|
x1, x2 | array_like | Input string arrays to be concatenated. Must be broadcastable to a common shape. |
out | ndarray, None, or tuple of ndarray and None, optional | Optional output array where the result is stored. If None, a new array is created. |
where | array_like, optional | Boolean mask specifying where to perform concatenation. Unselected elements retain their original value. |
casting | str, optional | Defines the casting behavior during computation. |
order | str, optional | Memory layout order of the output array. |
dtype | data-type, optional | Defines the data type of the output array. |
subok | bool, optional | Determines if subclasses of ndarray are preserved in the output. |
Return Value
Returns an array containing element-wise concatenated strings. If both inputs are scalars, a scalar is returned.
Examples
1. Concatenating Two String Scalars
Here, we concatenate two individual strings using numpy.strings.add().
</>
Copy
import numpy as np
# Define two string scalars
str1 = "apple"
str2 = "banana"
# Concatenate using numpy.strings.add
result = np.strings.add(str1, str2)
# Print the result
print("Concatenated string:", result)
Output:
Concatenated string: applebanana
