isspace() returns True when the string has at least one character and every character is whitespace. Whitespace means the space, the tab \t, the newline \n, the carriage return \r, and a few similar characters.
A string with any visible character, such as " a ", returns False. An empty string also returns False, which is the opposite of isascii() and isprintable().
text.isspace() is a method of the built-in str type: call it on a string with dot notation. No import is needed, and it never changes the original string.
Syntax
Call it on any string. There are no arguments, but the parentheses are required.
python
ok = text.isspace()
Call
What it does
" ".isspace()
Returns True. Every character is a space.
" \t\n".isspace()
Returns True. A space, a tab, and a newline are all whitespace.
"".isspace()
Returns False. An empty string has no character at all.
To remove whitespace from the ends use strip(). To split text on whitespace use split().
Library declarations
python
str.isspace() -> bool
# returns: True if the string is not empty and every character is whitespace
# an empty string returns False
Parameters
This method takes no arguments. It always checks the whole string.
Return Value
Return Value
Description
bool
True if the string is not empty and every character is whitespace, otherwise False.
Python Examples
Example 1 — Is This Text Only Whitespace?
Check spaces, a tab and a newline, an empty string, and a normal word.
python
for text in [" ", "\t\n", "", " a ", "go"]:
print(repr(text), text.isspace())
Only blanks. Spaces, and a tab with a newline, are all whitespace, so both give True.
Empty is False. The empty string has no character, so isspace() returns False.
One visible character." a " and "go" each contain a letter, so the answer is False.
Example 2 — Ignore Empty and Blank Lines
Keep only the lines that contain something visible.
python
lines = ["name", " ", "", "age", "\t"]
kept = [line for line in lines if line and not line.isspace()]
print(kept)
print([line for line in lines if line.strip()] == kept)
Output
['name', 'age']
True
How It Works
Two conditions. A line is kept if it is not empty and is not only whitespace.
Both blank kinds are dropped. The spaces, the empty string, and the tab are removed, leaving two lines.
A shorter way.line.strip() is an empty string, which counts as false, for both empty and blank lines, so the second list is the same.
Example 3 — Count the Whitespace Characters
Count every whitespace character of a sentence, then count the words.
python
text = "to be\tor\nnot to be"
gaps = sum(1 for ch in text if ch.isspace())
print(gaps)
print(len(text.split()))
Output
5
6
How It Works
Test every character. The generator adds 1 for each character whose isspace() is True.
Different kinds count. The text has 3 spaces, 1 tab, and 1 newline, which makes 5 whitespace characters.
Words are one more than gaps here.split() treats every run of whitespace as one separator and finds 6 words.
Frequently Asked Questions
It returns True if the string is not empty and every character is whitespace, such as a space, tab, or newline.
False. The string needs at least one character to count as whitespace.
Use not text.strip(). It is True for both an empty string and a string that holds only whitespace.
Yes. Spaces, tabs, newlines, carriage returns, form feeds, and vertical tabs are all whitespace.
isspace() only answers True or False. strip() returns a new string with the whitespace removed from both ends.