numpy - capitalize() and title()
In this numpy tutorial, we will discuss capitalize() and title() functins performed on numpy array.
Introduction
numpy stands for numeric python which is used to perform mathematical operations on arrays.
It is a module in which we have to import from the python.
Syntax to import:
import numpy
We can also use alias for the module
For example,
import numpy as np
We can directly use np to call the numpy module.
Array
An array is an one dimensional data structure used to store single data type data.
I.E It will only store all integer data or all string type data.or all float type data.
We can create an numpy array by using array() function.
Syntax:
numpy.array(elements)
where, elements are the input data elements.
This method will capitalize only first character element present in the numpy array.
Syntax:
numpy.char.capitalize(array_input)
where, array_input is the numpy input array
Example:
In this example, we will create an numpy array and capitalize the first character for each element.
#importing numpy module
import numpy
#create numpy array with 5 string elements
data=numpy.array(['php',"html css","jsp","go lang","css/jsp"])
#display the array
print(data)
#capitalize the array
print(numpy.char.capitalize(data))
Output:
We observed that in the second line of output, first character in each element is capitalized.
['php' 'html css' 'jsp' 'go lang' 'css/jsp']
['Php' 'Html css' 'Jsp' 'Go lang' 'Css/jsp']
This method will capitalize the every string element present in the numpy array.
Syntax:
numpy.char.title(array_input)
where, array_input is the numpy input array
Example:
In this example, we will create an numpy array and capitalize the each element.
#importing numpy module
import numpy
#create numpy array with 5 string elements
data=numpy.array(['php',"html css","jsp","go lang","css/jsp"])
#display the array
print(data)
#apply title() on the array
print(numpy.char.title(data))
Output:
['php' 'html css' 'jsp' 'go lang' 'css/jsp']
['Php' 'Html Css' 'Jsp' 'Go Lang' 'Css/Jsp']
Would you like to see your article here on tutorialsinhand.
Join
Write4Us program by tutorialsinhand.com
About the Author
Gottumukkala Sravan Kumar 171FA07058
B.Tech (Hon's) - IT from Vignan's University.
Published 1400+ Technical Articles on Python, R, Swift, Java, C#, LISP, PHP - MySQL and Machine Learning
Page Views :
Published Date :
Jun 14,2024