Testing string content on non-whitespace

python, whitespace

Solution

Strings have a method called `str.isspace` which, according to the docs:

Return[s] true if there are only whitespace characters in the string and there is at least one character, false otherwise.

So, that means:

if teststring.isspace():
    # contains only whitespace

Will do what you want.

Problem

I want to test if a sentence contains anything else than white-space characters. This is what I use currently: ``` if len(teststring.split()) > 0: # contains something else than white space else: # only white space ``` Is this good enough? Are there any better ways of doing it?

Original source