I am wondering if python has any function such as php empty function (http://php.net/manual/en/function.empty.php) which check if the variable is empty with following criteria
"" (an empty string) 0 (0 as an integer) 0.0 (0 as a float) "0" (0 as a string) NULL FALSE array() (an empty array)
PHP empty() Function The empty() function checks whether a variable is empty or not. This function returns false if the variable exists and is not empty, otherwise it returns true. The following values evaluates to empty: 0.
if str(variable) == [contains text]:
The None value is not an empty string in Python, and neither is (spaces).
See also this previous answer which recommends the not
keyword
How to check if a list is empty in Python?
It generalizes to more than just lists:
>>> a = "" >>> not a True >>> a = [] >>> not a True >>> a = 0 >>> not a True >>> a = 0.0 >>> not a True >>> a = numpy.array([]) >>> not a True
Notably, it will not work for "0" as a string because the string does in fact contain something - a character containing "0". For that you have to convert it to an int:
>>> a = "0" >>> not a False >>> a = '0' >>> not int(a) True
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With