str.isspace()
Added in v3.x · Updated March 13, 2026 · String Methods
string methods stdlib
The .isspace() method returns True if all characters in the string are whitespace characters and the string contains at least one character. Otherwise, it returns False.
Syntax
str.isspace()
This method does not take any parameters.
Return Value
Returns a boolean:
True— every character is a whitespace character and the string is not emptyFalse— otherwise
Whitespace Characters
Python considers these characters as whitespace:
| Character | Description |
|---|---|
Space ' ' | Regular space |
Tab '\t' | Horizontal tab |
Newline '\n' | Line feed |
Vertical tab '\v' | Vertical tab |
Form feed '\f' | Form feed |
Carriage return '\r' | Carriage return |
Examples
Basic Usage
>>> "hello".isspace()
False
>>> " ".isspace()
True
>>> "".isspace()
False
With Different Whitespace Characters
>>> "\t\n".isspace()
True
>>> " \t \n ".isspace()
True
>>> "hello world".isspace()
False
Practical Use Case
Checking if a string contains only whitespace is useful for input validation:
def process_input(user_input):
if not user_input.isspace():
return user_input.strip()
return None
>>> process_input(" ")
None
>>> process_input("hello")
'hello'
>>> process_input(" hello ")
'hello'
Distinguishing from .strip()
>>> s = " "
>>> s.isspace()
True
>>> len(s) > 0
True
>>> s = ""
>>> s.isspace()
False
>>> len(s) > 0
False
The key difference: an empty string returns False for .isspace() but True after .strip() produces an empty string.
Related Methods
.isprintable()— returnsTrueif all characters are printable.isalpha()— returnsTrueif all characters are alphabetic.isalnum()— returnsTrueif all characters are alphanumeric.strip()— removes leading and trailing whitespace.splitlines()— splits on line boundaries