Python isspace() is a built-in method returns true if there are only whitespace characters in the string. Otherwise, it returns false. Space, newline, tabs, etc., are known as whitespace characters and defined in the Unicode character database as Other or Separator.
The syntax of the method is: str.isspace()
Key Points :
• Return Type: Boolean i.e. True or False
• Parametric Values: There are no isspace() function parameters.
We’ll be covering the following topics in this tutorial:
Return Value from isspace()
isspace() method returns:
• Returns True if there are no white spaces on the input string.
• Returns False when one or more non-whitespace characters are included in the string.
Below is the python program to demonstrate the isspace() function:
str = " "; print str.isspace(); str = "The Best Learning Resource for Online Education"; print str.isspace();
When we run above the program, the outcome is as follows:
True
False
How to use isspace()?
str = '\t \n' if str.isspace() == True: print('All are whitespace characters') else: print('Contains non-whitespace characters') str = '5+5 = 10' if str.isspace() == True: print('All are whitespace characters') else: print('Contains non-whitespace characters.')
When we run above the program, the outcome is as follows:
All are whitespace characters
Contains non-whitespace characters.
Below are several other functions that we can use to work with string in Python 3