Signup/Sign In

NumPy title() function

In this tutorial, we will cover title() function of the Numpy library.

The title() function is used to convert an input string into a title cased version.

  • It is important to note that the title case words always start with an uppercase character and all remaining characters are in the lowercase. If a string has multiple words, then all the words are converted to title cased. For example, if we have a string "i love studytonight", if we use the title() function on this string, it will be changed to "I Love Studytonight".

  • This function calls str.title internally for all the elements of the array.

  • This function is locale-dependent for an 8-bit string.

Syntax of numpy.char.title():

The syntax required to use this function is as follows:

numpy.char.title(arr)

The above syntax indicates that title() function takes two parameters.

In the above syntax, the argument arr indicates the input array of strings on which this method will be applied.

Returned Values:

This function will return a titlecased string corresponding to the original string. If you provide an array of strings then all the string elements will be changed to titlecased.

Example 1: With a String

Here is a basic code example where we have used the title() function on a sstring:

import numpy as np  

a = "this is my string"
print("The original string:")
print(a)
print("\n")
print("Applying title() method :")  

x = np.char.title(a)
print(x)


The original string:
this is my string

Applying title() method :
This Is My String

Example 2:

In this example, we will use the title() function with a string which has some characters in upper case to see how the title() function changes it:

import numpy as np  

a = "Titlecased StrINg "
print("The original string:")
print(a)
print("\n")
print("Applying title() method :")  

x = np.char.title(a)
print(x)


The original string:
Titlecased StrINg

Applying title() method :
Titlecased String

Example 3: With array of strings

Now let's use the title() function with an array of string values:

import numpy as np

arr = np.array(['what aRE YOUR', 'plans for Tonight', 'will you','study tonight']) 
print ("The original Input array : \n", arr) 

output = np.char.title(arr)
print ("The output titlecased array: ", output)


The original Input array :
['what aRE YOUR' 'plans for Tonight' 'will you' 'study tonight']
The output titlecased array: ['What Are Your' 'Plans For Tonight' 'Will You' 'Study Tonight']

Summary

In this tutorial we learned about the title() function of the Numpy library. We covered how it is used with its syntax and values returned by this function along with a few examples to see how this function works with strings and array of strings.



About the author:
Aspiring Software developer working as a content writer. I like computer related subjects like Computer Networks, Operating system, CAO, Database, and I am also learning Python.