Shorter way to check if a string is not isdigit()

Python's "not" operand is not, not !.

Python's "logical not" operand is not, not !.


In python, you use the not keyword instead of !:

if not string.isdigit():
    do_stuff()

This is equivalent to:

if not False:
    do_stuff()

i.e:

if True:
    do_stuff()

Also, from the PEP 8 Style Guide:

Don't compare boolean values to True or False using ==.

Yes: if greeting:

No: if greeting == True

Worse: if greeting is True:


if not my_str.isdigit()

Also, don't use string as a variable name as it is also the name of a widely used standard module.

Tags:

Python