The `isspace()` function in Python is a string method that checks whether all the characters in a given string are whitespace characters. If the string contains only whitespace characters and is not empty, it returns `True`. Otherwise, it returns `False`.
Syntax
string.isspace()
Example Usage
Here are a few examples demonstrating the use of the `isspace()` function:
# Example 1: String with only whitespace characters str1 = " " print(str1.isspace()) # Output: True # Example 2: String with a mix of whitespace and non-whitespace characters str2 = " hello" print(str2.isspace()) # Output: False # Example 3: String with no whitespace characters str3 = "hello" print(str3.isspace()) # Output: False # Example 4: Empty string str4 = "" print(str4.isspace()) # Output: False
Explanation
Example 1: The string `” “` contains only whitespace characters (spaces), so `isspace()` returns `True`.
Example 2: The string `” hello”` contains both whitespace characters and alphabetic characters, so `isspace()` returns `False`.
Example 3: The string `”hello”` has no whitespace characters, so `isspace()` returns `False`.
Example 4: An empty string is checked, and since it contains no characters, `isspace()` returns `False`.
This function is useful when you need to check if a string is made up solely of spaces, tabs, or other whitespace characters.