How can I test if the string is empty in Python?
For example,
"<space><space><space>"
is empty, so is
"<space><tab><space><newline><space>"
, so is
"<newline><newline><newline><tab><newline>"
, etc.
How can I test if the string is empty in Python?
For example,
"<space><space><space>"
is empty, so is
"<space><tab><space><newline><space>"
, so is
"<newline><newline><newline><tab><newline>"
, etc.
yourString.isspace()
"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.
Alternatively, you could use
strippedString = yourString.strip()
And then check if strippedString is empty.
>>> tests = ['foo', ' ', '\r\n\t', '', None]
>>> [bool(not s or s.isspace()) for s in tests]
[False, True, True, True, True]
>>>