The isspace() function in python helps to check whether all the characters in the string are whitespace characters or not. The function returns true if the string full of white space characters (tabs, spaces, newline, etc.) else returns false.
string.isspace()
The isspace() method doesn't take any parameters.
The return value is always a boolean value. If the string is empty the function returns False.
Input | Return Value |
---|---|
All white space characters | True |
At least one non-whitespace character | False |
string = ' \t'
print(string.isspace())
string = ' a '
print(string.isspace())
string = ''
print(string.isspace())
Output:
True False False
string = '\t \n'
if string.isspace() == True:
print('String full of whitespace characters')
else:
print('String contains non-whitespace characters')
string = '15+3 = 18'
if string.isspace() == True:
print('String full of whitespace characters')
else:
print('String contains non-whitespace characters.')
Output:
String full of whitespace characters String contains non-whitespace characters