1👍
You can use or
; given a string s
, s or 0
will evaluate to 0
if s
is empty:
s = ''
print(float(s or 0)) # 0.0
s = '123'
print(float(s or 0)) # 123.0
This happens because (in python) x or y
returns x
if x
is true, and y
if x
is false. Since an empty string is "falsy", '' or 0
is equal to 0
.
Source:stackexchange.com