How to type negative number with .isdigit?

Use lstrip:

question.lstrip("-").isdigit()

Example:

>>>'-6'.lstrip('-')
'6'
>>>'-6'.lstrip('-').isdigit()
True

You can lstrip('+-') if you want to consider +6 a valid digit.

But I wouldn’t use isdigit, you can try int(question), it’ll throw an exception if the value cannot be represented as int:

try:
    int(question)
except ValueError:
    # not int

Leave a Comment