upper() function - NumPy
The numpy.char.upper() function is used to convert all characters in the elements of a NumPy string array to uppercase. It works element-wise and returns a new array with the modified strings.
This example shows how to use np.char.upper() to convert lowercase characters into uppercase.
import numpy as np
arr = np.array(['hello world'])
res = np.char.upper(arr)
print(res)
Output
['HELLO WORLD']
Syntax
numpy.char.upper(arr)
- Parameters: arr (array_like) -> Input array of strings.
- Return Value: ndarray -> Array with all string elements converted to uppercase.
Examples
Example 1: In this example, we convert alphanumeric strings to uppercase.
import numpy as np
arr = np.array(['p4q r', '4q rp', 'q rp4', 'rp4q'])
res = np.char.upper(arr)
print("Array:", arr)
print("Result:", res)
Output
Array: ['p4q r' '4q rp' 'q rp4' 'rp4q'] Result: ['P4Q R' '4Q RP' 'Q RP4' 'RP4Q']
Example 2: In this example, we convert a simple word array into uppercase.
import numpy as np
arr = np.array(['geeks', 'for', 'geeks'])
res = np.char.upper(arr)
print("Array:", arr)
print("Result:", res)
Output
Array: ['geeks' 'for' 'geeks'] Result: ['GEEKS' 'FOR' 'GEEKS']
Example 3: Here, we apply upper() to sentences with mixed cases.
import numpy as np
arr = np.array(['NumPy is Powerful', 'python is fun'])
res = np.char.upper(arr)
print("Array:", arr)
print("Result:", res)
Output
Array: ['NumPy is Powerful' 'python is fun'] Result: ['NUMPY IS POWERFUL' 'PYTHON IS FUN']