What is the numpy.char.capitalize() function in Python?

Overview

The char.capitalize() function in Python is used to convert only the first character of each element of an input array of string to uppercase.

Syntax

char.capitalize(a)

Parameter value

The char.capitalize() function takes a single and mandatory parameter value a, which represents the input array of strings that need to be capitalized.

Return value

The char.capitalize() function returns an array of strings or Unicode, depending on the input types.

Code example

import numpy as np
# creating an array
myarray = np.array(["hello", "theophilus ,", 'HOW', "are", "you today?"])
# implementing the char.capitalize() function
print(np.char.capitalize(myarray))

Code explanation

  • Line 1: We import the numpy module.
  • Line 4: We create an array myarray, using the array() function.
  • Line 7: We implement the char.capitalize() function on the array myarray and print the result to the console.

Free Resources