Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages

Python String isspace() method

Python String Isspace() Metho

In this article, we will dive into the working of Python String isspace() method.Python String has got vivid built-in methods to work with string input.

Getting started with Python String isspace()

The Python string isspace() method is used to check the presence of white spaces within the input string.

The white space characters include:

  • \n
  • \t
  • \v
  • \f
  • ‘ ‘
  • \r
  • etc.

It returns True if the input string contains only white spaces. Otherwise, the function returns False when the string contains one or more non-white-space characters.

Syntax:

input_string.isspace()

Example 1:

inp = 'Engineering_Discipline'

print(inp.isspace()) 

In the above example, as the input string contains no white space, the function returns False.

Output:

False

Example 2:

inp = '\t \v \n'
  
print(inp.isspace()) 
  

In this example, the input string consists only of white spaces. Thus, the function evaluates to True.

Output:

True

Example 3:

inp = '\thello everyone!!\n'
  
print(inp.isspace()) 
  

In this example, the input string contains a combination of white space characters and non-white-space characters i.e. it contains one or more non-white-space characters.

Thus, the function returns False.

Output:

False

NumPy isspace() method

Python NumPy module provides us with the numpy.char.isspace() method to check the presence of white spaces in the input elements of the array.

Syntax:

numpy.char.isspace(input_array)

The numpy.char.isspace() function checks for the presence of white spaces in the input array in an element-wise fashion.

That is, it checks it for every element of the array and returns true or false for each and every element present.

Example:

import numpy
  

inp_arr1 = numpy.array([ 'Science', 'Commerce', 'Arts'] ) 
print ("Elements of array1:\n", inp_arr1)  
  
res1 = numpy.char.isspace(inp_arr1) 
print ("Array1 after using isspace():\n", res1) 

inp_arr2 = numpy.array([ 'Sci\nence', 'Commerce\t', 'Arts'] ) 
print ("Elements of array2:\n", inp_arr2)  
  
res2 = numpy.char.isspace(inp_arr2) 
print ("Array2 after using isspace():\n", res2) 

inp_arr3 = numpy.array([ '\n\r', '\f\t', ' '] ) 
print ("Elements of array3:\n", inp_arr3)  
  
res3 = numpy.char.isspace(inp_arr3) 
print ("Array3 after using isspace():\n", res3) 

Output:

Elements of array1:
 ['Science' 'Commerce' 'Arts']
Array1 after using isspace():
 [False False False]

Elements of array2:
 ['Sci\nence' 'Commerce\t' 'Arts']
Array2 after using isspace():
 [False False False]

Elements of array3:
 ['\n\r' '\x0c\t' ' ']
Array3 after using isspace():
 [ True  True  True]


Pandas isspace() method

Pandas module includes isspace() function to check the white-space character strings in the entire data present in the Series or DataFrame.

Syntax:

Series.str.isspace()

The Series.str.isspace() method checks for the presence of the white space strings for every element and return True only for those elements.

Note: Series.str.isspace() method works only for string type elements. If the interpreter encounters any non-string value, it raises a ValueError Exception.

The above exception can be controlled using .astype() function. The .astype() function converts the non-string type data to string type.

Example:

 
import pandas 

inp_data = pandas.Series(['Jim', 'Jonny', ' ', '\t', 'Daisy', '\n']) 

res = inp_data.str.isspace() 

print(res) 

Output:

0    False
1    False
2     True
3     True
4    False
5     True
dtype: bool

Conclusion

In this article, we have understood the functioning of the Python string isspace() method.


References

Python isspace() method