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

ParameterTypeDescription
x1, x2array_likeInput string arrays to compare. 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 which elements to compare. Elements where where=False retain their original value.
castingstr, optionalDefines the casting behavior when comparing 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 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