NumPy strings.isupper()
The numpy.strings.isupper()
function checks whether all cased characters in a string array are uppercase and there is at least one character. If the condition is met, it returns True
, otherwise False
.
Syntax
numpy.strings.isupper(x, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True)
Parameters
Parameter | Type | Description |
---|---|---|
x | array_like, with StringDType, bytes_ or str_ dtype | Input array containing string elements to check. |
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 check. Elements where where=False retain their original value. |
casting | str, optional | Defines the casting behavior when checking string cases. |
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 a boolean array indicating whether each string in the input array consists entirely of uppercase characters and contains at least one character. If the input is a scalar, a scalar boolean value is returned.
Examples
1. Checking Uppercase for a Single String
In this example, we check if a single string is entirely in uppercase.
import numpy as np
# Define a single string
string = np.str_("HELLO")
# Check if the string is uppercase
result = np.strings.isupper(string)
# Print the result
print("Is the string uppercase?", result)
Output:
Is the string uppercase? True

2. Checking Uppercase for an Array of Strings
We check whether multiple strings in an array are uppercase.
import numpy as np
# Define an array of strings
strings = np.array(["HELLO", "world", "PYTHON", "123", "UPPERlower"])
# Check if each string is uppercase
result = np.strings.isupper(strings)
# Print the results
print("Input strings:", strings)
print("Uppercase check:", result)
Output:
Input strings: ['HELLO' 'world' 'PYTHON' '123' 'UPPERlower']
Uppercase check: [ True False True False False]

3. Using the out
Parameter
Using an output array to store results instead of creating a new array.
import numpy as np
# Define an array of strings
strings = np.array(["HELLO", "world", "PYTHON", "numpy"])
# Create an output array with the same shape
output_array = np.empty_like(strings, dtype=bool)
# Check uppercase and store the result in output_array
np.strings.isupper(strings, out=output_array)
# Print the results
print("Uppercase check stored in output array:", output_array)
Output:
Uppercase check stored in output array: [ True False True False]

4. Using the where
Parameter
Using a condition to check uppercase only for selected elements.
import numpy as np
# Define an array of strings
strings = np.array(["HELLO", "WORLD", "Python", "numpy"])
# Define a mask (check uppercase only where mask is True)
mask = np.array([True, False, True, True])
# Compute uppercase check where mask is True
result = np.strings.isupper(strings, where=mask)
# Print the results
print("Uppercase check with mask:", result)
Output:
Uppercase check with mask: [ True False False False]

The uppercase check is applied only where mask=True
. Other values retain their original state.