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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With