Python String isspace() Method
The isspace() method is used to check whether all characters in a string are whitespace characters.
Syntax
string.isspace()
Parameters
No parameters.
Return Value
Returns True if all characters in a string are whitespace characters.
Returns False if the string contains at least one non-whitespace character or is empty.
Whitespace Characters
Whitespace characters include:
- Space (
' ') - Tab (
'\t') - Newline (
'\n') - Carriage return (
'\r') - Vertical tab (
'\v') - From feed (
'\f')
Examples
Check if a String Contains Only Whitespace
print(" ".isspace()) # True
print("\t".isspace()) # True
print("\t\n".isspace()) # True
print(" Python ".isspace()) # False
print("".isspace()) # False (empty string)