Home »
Python »
Python Programs
Python numpy.char.capitalize() Method (With Examples)
By IncludeHelp Last updated : November 29, 2023
Python numpy.char.capitalize() Method
The capitalize() method is a built-in method of the char class in the numpy module, it is used to get the copy of a string with the first letter capitalized, element-wise. The method accepts a string or array of strings and returns an updated string where the first letter is in a capital case, if the first letter is not a character then no change in the string.
Module
The following module is required to use the capitalize() method:
import numpy as np
Syntax
The following is the syntax of the capitalize() method:
char.capitalize(arr)
Parameter(s)
The following are the parameter(s):
- arr: An array of strings.
Return Value
The return type of the capitalize() method is <class 'numpy.ndarray'>, it returns an updated array of strings with the first letter capitalized.
Example 1: Using with array of strings (one-word strings)
# Importing numpy module
import numpy as np
# Creating an array of strings
arr = ["hello,", "world!", "how", "are", "you?", "12good"]
# Printing the array
print("arr: ", arr)
# Using char.capitalize() method
result = np.char.capitalize(arr)
# Printing the result
print("\nThe result is:\n", result)
print("\nThe type of the result is:", type(result))
Output
The output of the above example is:
arr: ['hello,', 'world!', 'how', 'are', 'you?', '12good']
The result is:
['Hello,' 'World!' 'How' 'Are' 'You?' '12good']
The type of the result is: <class 'numpy.ndarray'>
Example 2: Using with array of strings (strings having multiple words)
# Importing numpy module
import numpy as np
# Creating an array of strings
arr = ["hello world", "123 how are you?"]
# Printing the array
print("arr: ", arr)
# Using char.capitalize() method
result = np.char.capitalize(arr)
# Printing the result
print("\nThe result is:\n", result)
print("\nThe type of the result is:", type(result))
Output
The output of the above example is:
arr: ['hello world', '123 how are you?']
The result is:
['Hello world' '123 how are you?']
The type of the result is: <class 'numpy.ndarray'>
Python NumPy Programs »