Best way to check if string is an integer?
Roy Smith
roy at panix.com
Sat Apr 5 22:23:27 EDT 2008
In article <mailman.2927.1207447793.9267.python-list at python.org>,
Steve Holden <steve at holdenweb.com> wrote:
> > This doesn't cater for negative integers.
> >
> No, it doesn't, but
>
> s.isdigit() or (s[0] in "+-" and s[1:].isdigit) # untested
>
> does.
I think this fails on " -1". So, then you start doing
s.strip().isdigit(), and then somebody else comes up with some other
unexpected corner case...
int(s) and catching any exception thrown just sounds like the best way.
More information about the Python-list
mailing list