如何检查文本是否为“空"? (空格,制表符,换行符)在Python中? [英] How to check if text is "empty" (spaces, tabs, newlines) in Python?
本文介绍了如何检查文本是否为“空"? (空格,制表符,换行符)在Python中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何在Python中测试字符串是否为空?
How can I test if the string is empty in Python?
例如,
"<space><space><space>"
为空,
"<space><tab><space><newline><space>"
,也是
"<newline><newline><newline><tab><newline>"
等
推荐答案
yourString.isspace()
如果字符串中仅包含空格字符并且至少包含一个字符,则返回true,否则返回false."
"Return true if there are only whitespace characters in the string and there is at least one character, false otherwise."
使用特殊情况将空字符串组合起来.
Combine that with a special case for handling the empty string.
或者,您可以使用
strippedString = yourString.strip()
然后检查strippedString是否为空.
And then check if strippedString is empty.
这篇关于如何检查文本是否为“空"? (空格,制表符,换行符)在Python中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文