The isspace method
The isspace
method checks if a string contains only spaces. In addition to spaces, the method recognizes the following characters - '\t'
(horizontal indent), '\n'
(new line), '\v'
(vertical indent), '\f'
(move down to the next page), '\r'
(return to the beginning of the line). Nothing is passed to the method parameters. The method returns the boolean values True
or False
.
Syntax
string.isspace()
Example
Let's check that our string consists only of whitespace characters:
txt = ' '
print(txt.isspace())
Result of code execution:
True
Example
Now let's say there are other symbols in the line:
txt = 'abc de'
print(txt.isspace())
As a result, we will see an error:
False
Example
Let's check if the character '\n'
is present in the string:
txt = '\n'
print(txt.isspace())
Result of code execution:
True