str.isspace()
Check whether a string consists entirely of whitespace — Unicode-aware, and False on empty.
Common call
if line.isspace():
Returns
True or False
Replaces
a manual `if not ch.isspace(): return False` loop
Watch out
empty string → False; non-breaking space DOES count; zero-width chars do NOT
str.isspace()
→ bool
Demo
Live evaluation
Try:
Inputs
stringstrthe string to test
Output
' '.isspace()
True
isspace() returns True when EVERY character is Unicode whitespace AND the string is non-empty. That includes tabs, newlines, carriage returns, form feeds, non-breaking space (U+00A0), and various exotic Unicode spaces. It does NOT include zero-width joiners or other invisible-but-non-space characters. The empty string returns False by convention.
Common patterns
Blank-line detection
Simpler than stripping and comparing to empty — and more explicit.
if line.isspace(): skip_blank_line()
Guard against blank input
Empty AND all-whitespace both signal "nothing meaningful".
if not text or text.isspace(): return None
Preserve intentional blanks
Distinguish "blank line" from "end of input" when parsing.
for line in lines: if line.isspace(): emit("blank") elif not line: break else: emit(line.strip())
Examples
1. Plain spaces
" ".isspace()
Returns
True2. Tabs and newlines
"\t\n".isspace()
Returns
True3. Contains a letter
" x ".isspace()
Returns
False4. Empty is False
"".isspace()
Returns
False5. Non-breaking space
"\u00a0".isspace()
Returns
True6. Zero-width joiner
"\u200d".isspace()
Returns
FalsePitfalls
1. Empty string returns False, not True
Same rule as isalpha, isdigit, isalnum — the empty case is False by convention. isspace requires at least one character.
Wrong expectation
"".isspace()
False
Combined check
not s or s.isspace()
covers both blank and empty
2. Non-breaking space DOES count as whitespace
U+00A0 (from HTML ` ` or Word processors) is Unicode whitespace, so isspace returns True on it. This surprises anyone who thinks "only these characters look like spaces".
Passes silently
"\u00a0".isspace()
True
Explicit filter
if s and all(c in " \t\n\r" for c in s): ...
ASCII whitespace only
3. Zero-width chars are NOT whitespace
Zero-width joiner (U+200D), zero-width space (U+200B), and byte-order mark (U+FEFF) are invisible but not Unicode whitespace. isspace returns False on them.
Invisible non-space
"\u200b".isspace()
False # zero-width space is NOT whitespace category
Explicit strip
import unicodedata s_clean = "".join(c for c in s if unicodedata.category(c) != "Cf")
strip format characters
4. A single visible character mixed in flips the result
One non-whitespace character makes the whole string non-whitespace. There is no "mostly whitespace" middle ground.
Assumed True
" x ".isspace()
False
Strip and re-check
s.strip() == ""
True when only whitespace present
When to use
Use it
- Detecting blank lines in text processing pipelines
- Guarding against "whitespace only" user input
- Preserving the distinction between blank lines and empty input
- Combined with startswith / endswith for indentation checks
Reach for something else
- Empty-OR-whitespace check → `not s or s.isspace()`
- ASCII-only whitespace → explicit character check
- Stripping whitespace → str.strip / lstrip / rstrip
- Splitting on whitespace → str.split() (default handles this)
Notes
Complexity
O(n) — one linear scan
Return
bool — True or False
CPython impl
Objects/unicodeobject.c :: unicode_isspace
Memory
No allocation
Thread-safe
Yes — strings are immutable
FAQ
Any character Unicode classifies with the White_Space property — spaces, tabs, newlines, carriage returns, form feeds, vertical tabs, non-breaking space, and a handful of exotic Unicode spaces. Zero-width and format characters do NOT count.
History
1.0
isspace() has been part of str since Python 1.0.
3.0
Full Unicode support — accepts every character Unicode classifies as whitespace.