Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Numpy char.lower() Function



The Numpy char.lower() function is used to convert all characters in each string element of an array to lowercase. It is useful for normalizing text data by ensuring consistent casing which can be particularly helpful in preprocessing text for tasks such as comparison or analysis.

This function processes each string in the input array individually by returning an array of the same shape with all characters in lowercase.

Syntax

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

numpy.char.lower(a)

Parameter

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

Return Value

This function returns an array with the same shape as the input, with each word converted into lowercase.

Example 1

Following is the basic example of Numpy char.lower() function in which all elements of the input array are converted into lowercase −

import numpy as np
arr = np.array(['WELCOME', 'TO', 'TUTORIALSPOINT', 'HAPPY LEARNING'])
lowercase_arr = np.char.lower(arr)
print(lowercase_arr)

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

['welcome' 'to' 'tutorialspoint' 'happy learning']

Example 2

In this example each string in the input array is converted to lowercase by transforming mixed case strings into a consistent lowercase format −

import numpy as np

arr = np.array(['hElLo', 'wOrLd'])
lowercase_arr = np.char.lower(arr)
print(lowercase_arr)

Here is the output of the converting mixed case strings of an array into lowercase −

['hello' 'world']

Example 3

Each string element in the array is converted to lowercase and the function handles the different shapes of the array seamlessly. Below is the example which shows conversion of 2d array elements into lowercase −

import numpy as np

arr = np.array([['HEllO world', 'GOOD MORnInG'], ['goOdbYe eVErYoNe', 'HAVe A NiCE dAY']])
lowercase_arr = np.char.lower(arr)
print(lowercase_arr)

Below is the output of applying lower() function to 2d array −

[['hello world' 'good morning']
 ['goodbye everyone' 'have a nice day']]
numpy_string_functions.htm
Advertisements