The isspace()
method is used to check if the string contains only whitespaces.
string.isspace()
True
:
False
in either of the following cases:
If any character of the string is not whitespace.
If the string is empty.
The whitespace includes spaces, line, and para separators.
# True - because all characters are whitespaecstring = ' ';print(string.isspace())# False - because all characters are not spacestring = 'L ';print(string.isspace())# False because the string is emptystring = '';print(string.isspace())# True - because all are white spacestring = '\n\n\n \t';print(string.isspace())
In the code above, we called the isspace
method on different strings. The isspace
method will return True
for the strings that only have whitespaces.