<p>我想测试一个句子是否包含除空格字符以外的任何内容。这是我目前使用的:</p>
<pre><code>if len(teststring.split()) > 0:
# contains something else than white space
else:
# only white space
</code></pre>
<p>这够好吗?有更好的方法吗?</p>
<p>为此,我将使用<a href="http://docs.python.org/library/stdtypes.html?highlight=strip#str.strip" rel="nofollow">strip()</a>函数。</p>
<pre><code> if teststring.strip():
# non blank line
else:
# blank line
</code></pre>