NumPy strings.less_equal()
The numpy.strings.less_equal() function compares two string arrays element-wise and returns a boolean array indicating whether each element in x1 is less than or equal to the corresponding element in x2.
Syntax
</>
Copy
numpy.strings.less_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 string arrays to compare. 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 which elements to compare. Elements where where=False retain their original value. |
casting | str, optional | Defines the casting behavior when comparing 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 indicating where x1 <= x2 element-wise. If both inputs are scalars, a single boolean value is returned.
Examples
1. Comparing Two Scalar Strings
Here, we compare two string values directly.
</>
Copy
import numpy as np
# Define two string scalars
str1 = "apple"
str2 = "banana"
# Compare the strings using less_equal
result = np.strings.less_equal(str1, str2)
# Print the result
print("Is 'apple' <= 'banana'? :", result)
Output:
Is 'apple' <= 'banana'? : True
