w3resource

NumPy String: numpy.char.upper() function

numpy.char.upper() function

The numpy.char.upper() function is used to convert all the lowercase characters in a given array or string to uppercase. It returns an array of the same shape.

The function is useful when we want to convert all lowercase characters in a string or array to uppercase to normalize the data.

Syntax:

numpy.char.upper(a)

Parameter:

Name Description Required /
Optional
a Input array. array_like, {str, unicode}

Return value:

Output array of str or unicode, depending on input type.

Example: Converting strings to uppercase using numpy.char.upper()

>>> import numpy as np
>>> x = np.array(['a1b c', '1bca', 'bca1']);
>>> x
array(['a1b c', '1bca', 'bca1'], dtype='<U5')
>>> np.char.upper(x)
array(['A1B C', '1BCA', 'BCA1'], dtype='<U5')

In the above example the numpy.char.upper() function converts a numpy array of strings to uppercase.

Pictorial Presentation:

NumPy String operation: upper() function

Example: Converting strings to uppercase using numpy.char.upper()

>>> import numpy as np
>>> x = np.array(['the', 'quick', 'brown', 'fox'])
>>> np.char.upper(x)
array(['THE', 'QUICK', 'BROWN', 'FOX'], dtype='<U5')

The above code demonstrates the use of the numpy.char.upper() function to convert a numpy array of strings to uppercase.

Python - NumPy Code Editor:

Previous: translate()
Next: zfill()



Follow us on Facebook and Twitter for latest update.