3
if var is not None and var !="" and var !=" ":
   # todo

can I write it like this?:

if var: 
   # todo

var is only String type.

doniyor
  • 36,596
  • 57
  • 175
  • 260

1 Answers1

13

If you want to filter out space-only string (" "):

if var and var.strip():
    # ...

Becasue string that contain spaces is evaludated as True if used as predicate:

>>> bool("")
False

>>> bool("  ")
True
falsetru
  • 357,413
  • 63
  • 732
  • 636