Last answered:

15 Mar 2022

Posted on:

17 Feb 2022

0

Resolved: why str(-11).isdigit() is false

I wanted to ask if I create a function and I want to make sure the argument passed to function is number.
How to do  that  ?
Also why does (-11).isdigit() returns False ?

1 answers ( 1 marked as helpful)
Instructor
Posted on:

15 Mar 2022

0

Hi Swarntam!
Thanks for reaching out.

The problem is, when you put negative numbers such as -11, the isdigit() method counts the - sign as a string value and it rejects it. So, we can slice it from the number. Please, use the following syntax:
str(-11)[1:].isdigit()

Hope this helps.
Best,
Tsvetelin

Submit an answer