pnina
pnina

Reputation: 119

Ignore case sensitive in Python

I want to check if a variable's value is one of 'Val' or 'val'. I can't use var.lower() because the variable can be None. I know I can use var in ['Val','val'] or str(var).lower() but I look for a nicer way...

Upvotes: 0

Views: 879

Answers (1)

hiro protagonist
hiro protagonist

Reputation: 46849

you can just combine those tests:

if var and var.lower() == 'val':

and short-circuits: if var is None, var.lower() is not executed.

or if var can also have other types and you want to be a bit more explicit:

if var is not None and var.lower() == 'val':

Upvotes: 2

Related Questions