Numpy char.upper() Function



The Numpy char.upper() function is used to convert all characters in each string element of an array to uppercase.

This function is useful for standardizing text data by ensuring that all characters are uniformly capitalized. It processes each string individually and returns an array of the same shape with all characters transformed to uppercase.

The char.upper() function is particularly helpful in text preprocessing tasks where consistent casing is needed for comparisons or other operations.

Syntax

Following is the syntax of Numpy char.upper() function −

numpy.char.upper(a)

Parameter

The Numpy char.upper() function accepts a single parameter namely, a, which is the input array with strings to be converted to uppercase.

Return Value

This function returns an array with the same shape as the input, with each letter converted to uppercase.

Example 1

Following is the basic example of Numpy char.upper() function in which all elements of the given input array is converted to upper case −

import numpy as np
arr = np.array(['welcome', 'to', 'tutorialspoint', 'happy learning'])
upper_arr = np.char.upper(arr)
print(upper_arr)

Below is the output of the basic example of numpy.char.upper() function −

['WELCOME' 'TO' 'TUTORIALSPOINT' 'HAPPY LEARNING']

Example 2

With the help of upper() function, each string in the input array is converted to uppercase by transforming mixed case strings into a consistent uppercase format. Here is the example of it −

import numpy as np

arr = np.array(['hElLo', 'wOrLd'])
upperd_arr = np.char.upper(arr)
print(upperd_arr)

Here is the output of converting mixed case strings −

['HELLO' 'WORLD']

Example 3

When we use the char.upper()function it converts all alphabetic characters in each string element of the array to uppercase while leaving numbers and punctuation unchanged. Below is the example −

import numpy as np

arr = np.array(['hello123!', 'world!!!'])
upper_arr = np.char.upper(arr)
print(upper_arr)

Below is the output of working with strings containing numbers and punctuation −

['HELLO123!' 'WORLD!!!']
numpy_string_functions.htm
Advertisements