What is string.isspace in Python?

The isspace() method is used to check if the string contains only whitespaces.

Syntax

string.isspace()

Return value

True:

  • If all the characters of the string are whitespace.

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.

Example

# True - because all characters are whitespaec
string = ' ';
print(string.isspace())
# False - because all characters are not space
string = 'L ';
print(string.isspace())
# False because the string is empty
string = '';
print(string.isspace())
# True - because all are white space
string = '\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.

Free Resources