Signup/Sign In

NumPy upper() function

In this tutorial, we will cover upper() function of the char module in the Numpy library.

The upper() function is used to convert all lowercase characters of a string to uppercase. If there is no lowercase characters in the given string then this function will return the original string.

  • This function usually calls str.upper on all the elements of the array.

  • This function is locale-dependent for an 8-bit string.

Syntax of numpy.char.upper():

The syntax required to use this function is as follows:

numpy.char.upper(arr)

The above syntax indicates that upper() function takes two parameters.

In the above syntax, the argument arr is mainly used to indicate the input array of the string on which this method will be applied.

Returned Values:

This function will return an uppercased string corresponding to the original string.

Example 1: With a string

In the code snippet below, we will use upper() function on a simple string:

import numpy as np  

a = "this is a string in numPy"
print("The original string:")
print(a)
print("\n")
print("Applying upper() method :")  

x = np.char.upper(a)
print(x)


The original string:
this is a string in numPy

Applying upper() method :
THIS IS A STRING IN NUMPY

Example 2:

Below we have a code snippet where we will use a string that is already in uppercase, then check the output for the same:

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


The original string:
THIS IS AN UPPERCASE STRING

Applying upper() method :
THIS IS AN UPPERCASE STRING

Example 3: With array of strings

Just like the above two examples, if we create an array of strings and then use this function with the array, it will convert all the string elements to upper case.

import numpy as np

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)


The original Input array :
['what aRE YOUR' 'plans for Tonight' 'will you' 'study tonight']
The output array: ['WHAT ARE YOUR' 'PLANS FOR TONIGHT' 'WILL YOU' 'STUDY TONIGHT']

Summary

In this tutorial 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.



About the author:
Aspiring Software developer working as a content writer. I like computer related subjects like Computer Networks, Operating system, CAO, Database, and I am also learning Python.