Python str.isspace() function
In Python, the str.isspace()
method is used to check if all characters in a string are whitespace characters. This method returns True
if the string consists only of whitespace characters and contains at least one character; otherwise, it returns False
.
Syntax
Example Usage
- Basic usage with spaces:
- Including other whitespace characters:
- Checking a string with mixed characters:
- Empty string:
- String with non-whitespace characters:
Important Notes
- The
isspace()
method recognizes various whitespace characters, including:- Space (
' '
) - Tab (
'\t'
) - Newline (
'\n'
) - Carriage return (
'\r'
) - Vertical tab (
'\v'
) - Form feed (
'\f'
)
- Space (
- It returns
False
for strings that contain any characters that are not whitespace.
Summary
- Use
str.isspace()
to determine if a string consists entirely of whitespace characters. - It returns
True
for strings made up solely of whitespace (including tabs and newlines) andFalse
for strings containing any non-whitespace characters.