Reputation: 344
I always understood that if something can be converted to integer (ie; something is string representation of numeric), isdigit() return True. This is not the case with the new feature. Here is the sample below:
But why?
Upvotes: 1
Views: 91
Reputation: 6748
You also need to take out the negative sign for negative integers. This way negative integers will work as well. str.replace("_", "").lstrip("-").isdigit()
.
Upvotes: 1
Reputation: 1807
To answer your question, looking at the python 3.6 documentation for the isdigit
method.
Return true if all characters in the string are digits and there is at least one character, false otherwise.
Since an underscore isn't a digit, the new format will not work well with the current implementation of isdigit
. As I commented before, the immediate work around would be: str.replace("_", "").isdigit()
where str is string containing the newly formatted number, while avoiding a try-except block with int
.
Upvotes: 1