Using the NumPy char.lower Function

PythonPythonBeginner
Practice Now

Introduction

In this lab, we will cover the usage of the char.lower() function in the NumPy library. This function is used to convert all uppercase characters of a string to lowercase characters. If there are no uppercase characters in the string, then the original string will be returned.

VM Tips

After the VM startup is done, click the top left corner to switch to the Notebook tab to access Jupyter Notebook for practice.

Sometimes, you may need to wait a few seconds for Jupyter Notebook to finish loading. The validation of operations cannot be automated because of limitations in Jupyter Notebook.

If you face issues during learning, feel free to ask Labby. Provide feedback after the session, and we will promptly resolve the problem for you.

Import NumPy Library

To begin, let's import the NumPy library.

import numpy as np

Convert Uppercase String to Lowercase

Let's use the char.lower() function to convert a string with uppercase characters to lowercase characters. In this example, we will use the string "THIS IS A STRING IN NUMPY".

original_string = "THIS IS A STRING IN NUMPY"
print("Original String: ", original_string)

new_string = np.char.lower(original_string)
print("New String: ", new_string)

Output:

Original String: THIS IS A STRING IN NUMPY
New String: this is a string in numpy

Do Not Modify Lowercase String

If we apply the char.lower() function on a string that is already in lowercase, then the function will return the same string without modifying it. Let's see an example:

original_string = "string1"
print("Original String: ", original_string)

new_string = np.char.lower(original_string)
print("New String: ", new_string)

Output:

Original String: string1
New String: string1

Convert Uppercase Strings in an Array to Lowercase

We can also use the char.lower() function to convert all uppercase characters in an array of strings to lowercase characters. Let's see an example:

arr = np.array(['what aRE YOUR', 'Plans for Tonight', 'WILL you', 'Studyonight'])
print("Original Array: ", arr)

lowercase_arr = np.char.lower(arr)
print("Lowercase Array: ", lowercase_arr)

Output:

Original Array: ['what aRE YOUR' 'Plans for Tonight' 'WILL you' 'Studyonight']
Lowercase Array: ['what are your' 'plans for tonight' 'will you' 'studyonight']

Understand Locale-Dependent 8-bit String

It's important to note that the char.lower() function is locale-dependent for an 8-bit string. This means that the function would work differently according to the location. Let's see an example:

original_string = "ß"
print("Original String: ", original_string)

new_string = np.char.lower(original_string)
print("New String: ", new_string)

Output:

Original String: ß
New String: 'ß'

Conclusion

In this lab, we have learned how to use the char.lower() function of the NumPy library. We learned that this function is used to convert all uppercase characters in a string or an array of strings to lowercase characters. We also learned that the function is locale-dependent for an 8-bit string.

Summary

Congratulations! You have completed the Using the NumPy char.lower() Function lab. You can practice more labs in LabEx to improve your skills.

Other Python Tutorials you may like