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

ParameterTypeDescription
x1, x2array_likeInput arrays containing strings. If their shapes differ, they must be broadcastable to a common shape.
outndarray, None, or tuple of ndarray and None, optionalOptional output array where the result is stored. If None, a new array is created.
wherearray_like, optionalBoolean mask specifying where the comparison should be applied.
castingstr, optionalDefines the casting behavior when comparing the strings.
orderstr, optionalMemory layout order of the output array.
dtypedata-type, optionalDefines the data type of the output array.
subokbool, optionalDetermines 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