Python Example - Check String is empty or not
The string is a group of characters(or Unicode) enclosed in single or double quotes in Python.
Check If the string is empty in Python
What makes a string empty?
The string contains empty characters enclosed in double or single quotes.
Python evaluates to Boolean Context that returns Falsy values for sequence types such as strings in conditional statements.
str="";
print(not str); # True
That means, space always returns True in the boolean Context
if String contains one or more spaces, Then It is not empty and returns False Values in Boolean Context.
str=" ";
print(not str); # False
So, if you use the sequences in conditional Statements It always returns True or false
Here is an example
str="";
if not str.strip():
print("string empty")
else:
print("string not empty")
The above approach works for empty or one or more spaces
The second way using eq function This is elegant and full proof using eq operator
It checks again empty string
str="";
print("".__eq__(str));
another way using ispace function
ispace function returns false for empty string and true for one or more empty space strings.
str="";
str1=" ";
print(str.isspace());
print(str1.isspace());
Conclusion
with the Above three approaches, the First approach is better and it checks empty or one or more spaces