NumPy isspace() function

In this tutorial, we will cover isspace() function in the char module of the Numpy Library in Python.

The isspace() function of the NumPy library returns True if all the characters in the element are whitespaces, otherwise, this function will return False.

  • This function calls str.isspace for each element of the array.

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

Syntax of the isspace():

The syntax required to use this function is as follows:

numpy.char.isspace(arr)

The above syntax indicates that isspace() function takes a single parameter.

In the above syntax, the argument arr is mainly used to indicate the input array of the strings on which this function will be applied.

Returned Values:

This function will return an output array of boolean values, with True and False values corresponding to every string element, based on whether the string has only whitespaces or not.

Example 1:

In the example below, we will use this function with strings which have spaces and some alphabets too.

import numpy as np

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)


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

Example 2:

In the below code snippet we will use isspace() function with strings with whitespaces and line breaks:

import numpy as np

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)


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

Summary

In this tutorial we learned about isspace() function in the Numpy Library. We had covered how it is used with its syntax and values returned by this function.