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

Antoon Pardon apardon at forel.vub.ac.be
Wed Dec 21 07:40:13 EST 2005


Op 2005-12-21, Steven D'Aprano schreef <steve at REMOVETHIScyber.com.au>:
> On Wed, 21 Dec 2005 03:37:27 -0800, Neuruss wrote:
>
>> Can't we just check if the string has digits?
>
> Why would you want to?
>
>
>> For example:
>> 
>>>>> 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.

So? the isdigit method tests whether all characters are digits.

>>> '15'.isdigit()
True

-- 
Antoon Pardon



More information about the Python-list mailing list