NumPy Isspace Function

NumPyNumPyBeginner
Practice Now

Introduction

NumPy is a popular library for performing numerical computations in Python. In this lab, we will cover the isspace() function in the char module of the NumPy Library. The isspace() function is used to check if all the characters in an element are whitespace characters. This lab will provide you a step-by-step guide on how to use the function in Python.

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/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-86464{{"`NumPy Isspace Function`"}} python/tuples -.-> lab-86464{{"`NumPy Isspace Function`"}} python/importing_modules -.-> lab-86464{{"`NumPy Isspace Function`"}} python/standard_libraries -.-> lab-86464{{"`NumPy Isspace Function`"}} python/numerical_computing -.-> lab-86464{{"`NumPy Isspace Function`"}} python/build_in_functions -.-> lab-86464{{"`NumPy Isspace Function`"}} numpy/data_array -.-> lab-86464{{"`NumPy Isspace Function`"}} numpy/bool_idx -.-> lab-86464{{"`NumPy Isspace Function`"}} numpy/fancy_idx -.-> lab-86464{{"`NumPy Isspace Function`"}} end

Importing the Required Libraries

Before using the isspace() function, we need to import the NumPy library. We can do this with the following code snippet:

import numpy as np

Using the isspace() function

The isspace() function of the char module of the NumPy library returns True if all the characters in the element are whitespace characters, otherwise, it will return False. To use this function, we need to call np.char.isspace() and pass an array of strings to it.

Syntax:

np.char.isspace(arr)

Here, arr is the input array of strings on which this function will be applied.

The isspace() function returns an output array of Boolean values with True and False values corresponding to every string element based on whether the string has only whitespace characters or not.

Example 1

In this example, we will use the isspace() function with strings that contain spaces and some alphabets as well.

inp_ar = np.array([ 'Superb !', 'Amazing!'] )
print("The Input string is:")
print(inp_ar)

x = np.char.isspace(inp_ar)
print("The Output is:")
print(x)

Output:

The Input string is:
['Superb !' 'Amazing!']
The Output is:
[False False]

Example 2

In this example, we will use the isspace() function with strings that have whitespace characters and line breaks:

inp_ar = np.array([ '\n', '\t',' ','abc nb'] )
print("The Input string is:")
print(inp_ar)

x = np.char.isspace(inp_ar)
print("The Output is:")
print(x)

Output:

The Input string is:
['\n' '\t' ' ' 'abc nb']
The Output is:
[ True True True False]

Summary

In this lab, we learned about the isspace() function in the NumPy Library. We covered how it is used with its syntax and the boolean values returned by this function. The isspace() function is easy to use and can be helpful in many situations when we want to check if strings have whitespace characters.

Other NumPy Tutorials you may like