NumPy isspace() function

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)

Output:


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)

Output:


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