Python String isspace()

Python String isspace() is used to check if given string contains only whitespace characters.

isspace() method returns True if all the characters in the given string are whitespaces like single space, new line, tab space, etc., or else the method returns False.

In this tutorial, we will learn the syntax and examples for isspace() method of String class.

Syntax

The syntax to call isspace() method on string x in Python is

</>
Copy
x.isspace()

Examples

In the following program, we take a string ' \n\t' in x, and check if this string x contains only spaces using isspace() method.

Example.py

</>
Copy
x = ' \n\t'
result = x.isspace()
print(result)

Output

True

In the following program, we take a string 'a \n\tb' in x, and check if this string x contains only spaces using isspace() method. Since, there are non-whitespace characters a, b in the string, the method returns False.

Example.py

</>
Copy
x = 'a \n\tb'
result = x.isspace()
print(result)

Output

False

Conclusion

In this Python Tutorial, we learned how to check if given string contains only whitespace characters, using String method – isspace().