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

2. Comparing Two Arrays of Strings
Checking element-wise equality for two string arrays.
</>
Copy
import numpy as np
# Define two arrays of strings
arr1 = np.array(["apple", "banana", "cherry"])
arr2 = np.array(["apple", "orange", "cherry"])
# Perform element-wise comparison
result = np.strings.equal(arr1, arr2)
# Print the result
print("Comparison result:", result)
Output:
Comparison result: [ True False True]

3. Using Broadcasting to Compare a Single String with an Array
We compare a single string with each element of an array using broadcasting.
</>
Copy
import numpy as np
# Define an array of strings
arr = np.array(["apple", "banana", "cherry"])
# Compare each element with a single string
result = np.strings.equal(arr, "banana")
# Print the result
print("Comparison with 'banana':", result)
Output:
Comparison with 'banana': [False True False]

4. Using the out
Parameter
Storing the comparison result in a predefined output array.
</>
Copy
import numpy as np
# Define two arrays of strings
arr1 = np.array(["apple", "banana", "cherry"])
arr2 = np.array(["apple", "grape", "cherry"])
# Create an output array
output_array = np.empty_like(arr1, dtype=bool)
# Perform element-wise comparison and store result in output_array
np.strings.equal(arr1, arr2, out=output_array)
# Print the result
print("Comparison result:", output_array)
Output:
Comparison result: [ True False True]

5. Using the where
Parameter
Applying conditional element-wise comparison.
</>
Copy
import numpy as np
# Define two arrays of strings
arr1 = np.array(["apple", "banana", "cherry"])
arr2 = np.array(["apple", "grape", "cherry"])
# Define a mask to specify where to perform comparison
mask = np.array([True, False, True])
# Compare strings only where mask is True
result = np.strings.equal(arr1, arr2, where=mask)
# Print the result
print("Comparison with mask:", result)
Output:
Comparison with mask: [ True False True]

The comparison is performed only where mask=True
, leaving the rest unchanged.