What is isspace() in Python?

The isspace() method is a string function in Python that tells whether or not a string has only whitespaces.

Syntax

It is declared as follows:

string.isspace()

Parameters

The isspace() method does not take any parameters.

Return value

Its return value is a Boolean, which is True if the string contains whitespaces only. The return value is False if it does not contain whitespaces or if it contains characters apart from whitespaces.

Code

The isspace() function returns True for the string that only contains whitespaces in the examples below. However, it returns’ False’ for the empty string and the string with both characters and whitespaces.

# string with whitespaces only
string = ' '
print(string.isspace())
# string with whitespaces and other characters
string = 'a b c 2 '
print(string.isspace())
# empty string
string = ''
print(string.isspace())
New on Educative
Learn to Code
Learn any Language as a beginner
Develop a human edge in an AI powered world and learn to code with AI from our beginner friendly catalog
🏆 Leaderboard
Daily Coding Challenge
Solve a new coding challenge every day and climb the leaderboard

Free Resources