Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
Message from discussion How to check if a string "is" an int?

Path: g2news1.google.com!news4.google.com!border1.nntp.dca.giganews.com!nntp.giganews.com!local01.nntp.dca.giganews.com!nntp.speakeasy.net!news.speakeasy.net.POSTED!not-for-mail
NNTP-Posting-Date: Wed, 21 Dec 2005 15:55:47 -0600
Date: Wed, 21 Dec 2005 13:55:47 -0800
From: Erik Max Francis <m...@alcyone.com>
Organization: Alcyone Systems
User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.7.5) Gecko/20041217
X-Accept-Language: en, eo
MIME-Version: 1.0
Newsgroups: comp.lang.python
Subject: Re: How to check if a string "is" an int?
References: <1135159955.302316.24060@g43g2000cwa.googlegroups.com>   <pan.2005.12.21.11.27.49.792746@REMOVETHIScyber.com.au> <1135165047.116025.325690@g43g2000cwa.googlegroups.com>
In-Reply-To: <1135165047.116025.325690@g43g2000cwa.googlegroups.com>
Content-Type: text/plain; charset=us-ascii; format=flowed
Content-Transfer-Encoding: 7bit
Message-ID: <qvOdnbcWh97-UjTenZ2dnUVZ_sOdnZ2d@speakeasy.net>
Lines: 28
NNTP-Posting-Host: 69.17.54.62
X-Trace: sv3-ZEKx8hwQcbhODB8ETpAXAlKWR7SgvBSAN3M9hVxtrh7Ezf7T/nsxREuhxenLmhka/qDniJzW6jGyekh!OE7vZiEHT+W5silvbb7KmBuzrj5JdMXeAc/C6S8ShPgEF/OCfWjai1DMW8yfawD9P7fgbG2tSOo4!TOwql8BxZxX9tMBz1W7XzrZu
X-Complaints-To: abuse@speakeasy.net
X-DMCA-Complaints-To: ab...@speakeasy.net
X-Abuse-and-DMCA-Info: Please be sure to forward a copy of ALL headers
X-Abuse-and-DMCA-Info: Otherwise we will be unable to process your complaint properly
X-Postfilter: 1.3.32

Neuruss wrote:

> Can't we just check if the string has digits?
> For example:
> 
>>>> x = '15'
>>>> if x.isdigit():
> 	print int(x)*3
> 
> 	
> 45

 >>> x = '-1'
 >>> if x.isdigit(): print int(x)*3
...

To make sure you get it right, you'll have to do exactly what the Python 
parser does in order to distinguish integer literals from other tokens. 
  Taken to the extreme for other types, such as floats, you're far 
better off just using the internal mechanisms that Python itself uses, 
which means to try to convert it and catch any exception that results 
from failure.

-- 
Erik Max Francis && m...@alcyone.com && http://www.alcyone.com/max/
San Jose, CA, USA && 37 20 N 121 53 W && AIM erikmaxfrancis
   Make it come down / Like molasses rain
   -- Sandra St. Victor