The string can be checked by checking for occurrences of only whitespace characters. We can check if a string contains only whitespace characters using 2 methods. First is using method isspace().
print('Hello world'.isspace()) print(' '.isspace())
False True
You can also use regexes for the same result. For matching only whitespace, we can call the re.match(regex, string) using the regex metacharacter \s as follows: "^\s*$".
import re print(bool(re.match('^\s+$', ' abc'))) print(bool(re.match('^\s+$', ' ')))
False True