NumPy Upper Function

PythonPythonBeginner
Practice Now

Introduction

In this lab, you will learn how to use the upper() function in the char module of the NumPy library. This function is used to convert all lowercase characters of a string to uppercase. If there are no lowercase characters in the given string, it returns the original string. We will cover the syntax required to use this function, its returned values, and provide examples of its use.

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/ModulesandPackagesGroup -.-> python/standard_libraries("`Common Standard Libraries`") 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-86516{{"`NumPy Upper Function`"}} python/lists -.-> lab-86516{{"`NumPy Upper Function`"}} python/tuples -.-> lab-86516{{"`NumPy Upper Function`"}} python/importing_modules -.-> lab-86516{{"`NumPy Upper Function`"}} python/standard_libraries -.-> lab-86516{{"`NumPy Upper Function`"}} python/numerical_computing -.-> lab-86516{{"`NumPy Upper Function`"}} python/build_in_functions -.-> lab-86516{{"`NumPy Upper Function`"}} numpy/1d_array -.-> lab-86516{{"`NumPy Upper Function`"}} numpy/data_array -.-> lab-86516{{"`NumPy Upper Function`"}} numpy/bool_idx -.-> lab-86516{{"`NumPy Upper Function`"}} numpy/fancy_idx -.-> lab-86516{{"`NumPy Upper Function`"}} end

Import NumPy Library

The first step is to import the NumPy library using the syntax below:

import numpy as np

Apply upper() function on a Simple String

In this step, we will use the upper() function on a simple string. We will create a variable a that contains the string "this is a string in NumPy" and apply the upper() function on it. Here is an example code snippet:

a = "this is a string in NumPy"
print("The original string:")
print(a)
print("\n")
print("Applying the upper() method:")
x = np.char.upper(a)
print(x)

Apply upper() function on a String in Uppercase

In this step, we will use a string that is already in uppercase, then check the output for the same. We will create a variable a that contains the string "THIS IS AN UPPERCASE STRING" and apply the upper() function on it. Here is an example code snippet:

a="THIS IS AN UPPERCASE STRING"
print("The original string:")
print(a)
print("\n")
print("Applying the upper() method:")
x=np.char.upper(a)
print(x)

Apply upper() function on an Array of Strings

In this step, we will create an array of strings and then use the upper() function with the array. It will convert all the string elements to uppercase. We will create an array arr that contains the strings "what aRE YOUR", "plans for Tonight", "will you", and "study tonight" and apply the upper() function on it. Here is an example code snippet:

arr = np.array(['what aRE YOUR', 'plans for Tonight', 'will you','study tonight'])
print ("The original Input array : \n", arr)

output = np.char.upper(arr)
print ("The output array: ", output)

Summary

In this lab, we covered the upper() function in the NumPy library. We covered how it is used with its syntax and values returned by this function along with multiple code examples. By following the above steps, you now have a better understanding of how to use the upper() function in the char module of the NumPy library.

Other Python Tutorials you may like