How to check if a string "is" an int?

Kent Johnson kent at kentsjohnson.com
Wed Dec 21 07:36:15 EST 2005


Steven D'Aprano wrote:
> On Wed, 21 Dec 2005 03:37:27 -0800, Neuruss wrote:
>>>>>x = '15'
>>>>>if x.isdigit():
>>
>>	print int(x)*3
> 
> 
> 15 is not a digit. 1 is a digit. 5 is a digit. Putting them together to
> make 15 is not a digit.

Maybe so, but '15'.isdigit() == True:

isdigit(...)
     S.isdigit() -> bool

     Return True if all characters in S are digits
     and there is at least one character in S, False otherwise.

  >>> '15'.isdigit()
True

though your other points are valid and I agree this is not the right solution to the OP.

Kent




More information about the Python-list mailing list