Does Python have something like an empty string variable where you can do?:
if myString == string.empty:
Regardless what's the most elegant way to check for empty string values? I find hard coding ""
every time for checking an empty string not as good.
You may have a look at this Assigning empty value or string in Python
This is about comparing strings that are empty. So instead of testing for emptiness with
not
, you may test is your string is equal to empty string with""
the empty string...When you are reading file by lines and want to determine, which line is empty, make sure you will use
.strip()
, because there is new line character in "empty" line:I would test noneness before stripping. Also, I would use the fact that empty strings are False (or Falsy). This approach is similar to Apache's StringUtils.isBlank or Guava's Strings.isNullOrEmpty
This is what I would use to test if a string is either None OR Empty OR Blank:
And, the exact opposite to test if a string is not None NOR Empty NOR Blank:
More concise forms of the above code: