NumPy Istitle Function

PythonPythonBeginner
Practice Now

Introduction

In this tutorial, we will learn about the istitle() function of the char module in the NumPy library. This function is used to determine if a given element in the input array of strings is title cased or not.

VM Tips

After the VM startup is done, click the top left corner to switch to the Notebook tab to access Jupyter Notebook for practice.

Sometimes, you may need to wait a few seconds for Jupyter Notebook to finish loading. The validation of operations cannot be automated because of limitations in Jupyter Notebook.

If you face issues during learning, feel free to ask Labby. Provide feedback after the session, and we will promptly resolve the problem for you.

Import NumPy Library

To use the istitle() function, we need to first import the NumPy library. We will import it using the standard alias np.

import numpy as np

Create an Input Array

Now, we will create an input array of strings that we will pass to the istitle() function. In this example, we will create a NumPy array inp1 with three elements: 'APPLE', 'Mango', 'guava'.

inp1 = np.array(['APPLE', 'Mango', 'guava'])

Apply the istitle() Function

Once we have created the input array, we can pass it to the istitle() function. This function will return a Boolean array with the same shape as the input array.

out1 = np.char.istitle(inp1)

View Output Array

Finally, we will print the output array that we obtained from using the istitle() function.

print("The input array: ", inp1)
print("The output array:", out1)

Complete code for Step 2-4

inp1 = np.array(['APPLE', 'Mango', 'guava'])
out1 = np.char.istitle(inp1)

print("The input array: ", inp1)
print("The output array:", out1)

The output of the above code will be:

The input array:  ['APPLE' 'Mango' 'guava']
The output array: [False  True False]

Use istitle() Function with a String

In this step, we will use the istitle() function with a string 'This Is An Input String'. This string will be passed to the istitle() function, which should return True since the string is titlecased.

inp2 = "This Is An Input String"
out2 = np.char.istitle(inp2)

print("The input string : \n", inp2)
print("The output from 'istitle()' function :\n", out2)

Complete code for Step 5

inp2 = "This Is An Input String"
out2 = np.char.istitle(inp2)

print("The input string : \n", inp2)
print("The output from 'istitle()' function :\n", out2)

The output of the above code will be:

The input string :
This Is An Input String
The output from 'istitle()' function :
True

Summary

In this tutorial, we learned about the istitle() function in the NumPy library. We covered how to use this function, its syntax, its input arguments, and the output returned by it. We also demonstrated how to use this function with an input array and a single input string. Finally, we saw how the istitle() function can be used to check if an input string is titlecased or not.

Other Python Tutorials you may like