NumPy strings.equal()
The numpy.strings.equal() function performs an element-wise comparison between two input arrays of strings and returns a boolean array indicating whether corresponding elements are equal.
Syntax
</>
Copy
numpy.strings.equal(x1, x2, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True)
Parameters
| Parameter | Type | Description |
|---|---|---|
x1, x2 | array_like | Input arrays containing strings. If their shapes differ, they 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 the comparison should be applied. |
casting | str, optional | Defines the casting behavior when comparing the strings. |
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 of boolean values where each element indicates whether the corresponding elements of x1 and x2 are equal. If both inputs are scalars, a single boolean value is returned.
Examples
1. Comparing Two Identical Strings
Checking whether two string values are equal.
</>
Copy
import numpy as np
# Define two string values
str1 = "apple"
str2 = "apple"
# Compare the two strings
result = np.strings.equal(str1, str2)
# Print the result
print("Are the strings equal?", result)
Output:
Are the strings equal? True
