NumPy strings.lower()

The numpy.strings.lower() function converts all characters in an array of strings to lowercase, applying the str.lower() method element-wise.

Syntax

</>
Copy
numpy.strings.lower(a)

Parameters

ParameterTypeDescription
aarray-like (StringDType, bytes_, or str_ dtype)Input array containing strings that need to be converted to lowercase.

Return Value

Returns an array with the same shape as the input, where all string elements are converted to lowercase.


Examples

1. Converting a Single String to Lowercase

Here, we convert a single uppercase string to lowercase.

</>
Copy
import numpy as np

# Define a single string
word = np.array("APPLE", dtype="str")

# Convert to lowercase
lower_word = np.strings.lower(word)

# Print the result
print("Lowercase string:", lower_word)

Output:

Lowercase string: apple

2. Converting an Array of Strings to Lowercase

We apply the numpy.strings.lower() function to an array of mixed-case strings.

</>
Copy
import numpy as np

# Define an array of strings
fruits = np.array(["APPLE", "BANANA", "Cherry", "dAtE"], dtype="str")

# Convert all strings to lowercase
lower_fruits = np.strings.lower(fruits)

# Print the results
print("Original array:", fruits)
print("Lowercase array:", lower_fruits)

Output:

Original array: ['APPLE' 'BANANA' 'Cherry' 'dAtE']
Lowercase array: ['apple' 'banana' 'cherry' 'date']

3. Handling Empty Strings and Special Characters

Demonstrating how numpy.strings.lower() handles empty strings and special characters.

</>
Copy
import numpy as np

# Define an array with special characters and empty strings
special_strings = np.array(["APPLE!", "  BANANA  ", "", "chErry123"], dtype="str")

# Convert to lowercase
lower_special_strings = np.strings.lower(special_strings)

# Print the results
print("Original array:", special_strings)
print("Lowercase array:", lower_special_strings)

Output:

Original array: ['APPLE!' '  BANANA  ' '' 'chErry123']
Lowercase array: ['apple!' '  banana  ' '' 'cherry123']

Note that the function preserves spaces and special characters while converting only the letters to lowercase.