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.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL python(("`Python`")) -.-> python/DataStructuresGroup(["`Data Structures`"]) python(("`Python`")) -.-> python/ModulesandPackagesGroup(["`Modules and Packages`"]) python(("`Python`")) -.-> python/DataScienceandMachineLearningGroup(["`Data Science and Machine Learning`"]) python(("`Python`")) -.-> python/FunctionsGroup(["`Functions`"]) numpy(("`NumPy`")) -.-> numpy/ArrayBasicsGroup(["`Array Basics`"]) numpy(("`NumPy`")) -.-> numpy/IndexingandSlicingGroup(["`Indexing and Slicing`"]) python/DataStructuresGroup -.-> python/lists("`Lists`") python/DataStructuresGroup -.-> python/tuples("`Tuples`") python/ModulesandPackagesGroup -.-> python/importing_modules("`Importing Modules`") python/ModulesandPackagesGroup -.-> python/standard_libraries("`Common Standard Libraries`") python/DataScienceandMachineLearningGroup -.-> python/numerical_computing("`Numerical Computing`") python/FunctionsGroup -.-> python/build_in_functions("`Build-in Functions`") numpy/ArrayBasicsGroup -.-> numpy/1d_array("`1D Array Creation`") numpy/ArrayBasicsGroup -.-> numpy/data_array("`Data to Array`") numpy/IndexingandSlicingGroup -.-> numpy/bool_idx("`Boolean Indexing`") numpy/IndexingandSlicingGroup -.-> numpy/fancy_idx("`Fancy Indexing`") subgraph Lab Skills python/lists -.-> lab-86466{{"`NumPy Istitle Function`"}} python/tuples -.-> lab-86466{{"`NumPy Istitle Function`"}} python/importing_modules -.-> lab-86466{{"`NumPy Istitle Function`"}} python/standard_libraries -.-> lab-86466{{"`NumPy Istitle Function`"}} python/numerical_computing -.-> lab-86466{{"`NumPy Istitle Function`"}} python/build_in_functions -.-> lab-86466{{"`NumPy Istitle Function`"}} numpy/1d_array -.-> lab-86466{{"`NumPy Istitle Function`"}} numpy/data_array -.-> lab-86466{{"`NumPy Istitle Function`"}} numpy/bool_idx -.-> lab-86466{{"`NumPy Istitle Function`"}} numpy/fancy_idx -.-> lab-86466{{"`NumPy Istitle Function`"}} end

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