Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Testing string content on non-whitespace

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?

like image 320
John Manak Avatar asked Nov 29 '22 09:11

John Manak


1 Answers

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.

like image 105
lvc Avatar answered Dec 06 '22 11:12

lvc