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.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL python(("`Python`")) -.-> python/ControlFlowGroup(["`Control Flow`"]) python(("`Python`")) -.-> python/DataStructuresGroup(["`Data Structures`"]) python(("`Python`")) -.-> python/ModulesandPackagesGroup(["`Modules and Packages`"]) python(("`Python`")) -.-> python/DataScienceandMachineLearningGroup(["`Data Science and Machine Learning`"]) python(("`Python`")) -.-> python/FunctionsGroup(["`Functions`"]) numpy(("`NumPy`")) -.-> numpy/ArrayBasicsGroup(["`Array Basics`"]) numpy(("`NumPy`")) -.-> numpy/IndexingandSlicingGroup(["`Indexing and Slicing`"]) python/ControlFlowGroup -.-> python/for_loops("`For Loops`") python/DataStructuresGroup -.-> python/lists("`Lists`") python/DataStructuresGroup -.-> python/tuples("`Tuples`") python/ModulesandPackagesGroup -.-> python/importing_modules("`Importing Modules`") python/DataScienceandMachineLearningGroup -.-> python/numerical_computing("`Numerical Computing`") python/FunctionsGroup -.-> python/build_in_functions("`Build-in Functions`") numpy/ArrayBasicsGroup -.-> numpy/1d_array("`1D Array Creation`") numpy/ArrayBasicsGroup -.-> numpy/data_array("`Data to Array`") numpy/IndexingandSlicingGroup -.-> numpy/bool_idx("`Boolean Indexing`") numpy/IndexingandSlicingGroup -.-> numpy/fancy_idx("`Fancy Indexing`") subgraph Lab Skills python/for_loops -.-> lab-86477{{"`Using the NumPy char.lower Function`"}} python/lists -.-> lab-86477{{"`Using the NumPy char.lower Function`"}} python/tuples -.-> lab-86477{{"`Using the NumPy char.lower Function`"}} python/importing_modules -.-> lab-86477{{"`Using the NumPy char.lower Function`"}} python/numerical_computing -.-> lab-86477{{"`Using the NumPy char.lower Function`"}} python/build_in_functions -.-> lab-86477{{"`Using the NumPy char.lower Function`"}} numpy/1d_array -.-> lab-86477{{"`Using the NumPy char.lower Function`"}} numpy/data_array -.-> lab-86477{{"`Using the NumPy char.lower Function`"}} numpy/bool_idx -.-> lab-86477{{"`Using the NumPy char.lower Function`"}} numpy/fancy_idx -.-> lab-86477{{"`Using the NumPy char.lower Function`"}} end

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