Python string Method - isspace()

w‮w‬w.theitroad.com

The isspace() method is a built-in string method in Python that returns True if all the characters in the string are whitespace characters. If the string is empty or contains no whitespace characters, the method returns False.

Whitespace characters are the characters that represent spaces, tabs, and line breaks.

Here is the syntax of the isspace() method:

str.isspace()

The isspace() method does not take any arguments. It can be called on a string and returns a Boolean value.

Example:

# Example 1
string1 = "    "
print(string1.isspace()) # Output: True

# Example 2
string2 = "Hello, World!"
print(string2.isspace()) # Output: False

# Example 3
string3 = "\t\n"
print(string3.isspace()) # Output: True

In the above example, string1 contains only whitespace characters, so isspace() method returns True. string2 contains non-whitespace characters, so isspace() method returns False. string3 contains only newline and tab characters, which are whitespace characters, so isspace() method returns True.