Martin Marcher wrote:
> hmmm
>
> int() does miss some stuff:
>
1E+1
> 10.0
int("1E+1")
> Traceback (most recent call last):
> File "", line 1, in
> ValueError: invalid literal for int() with base 10: '1E+1'
>
> I wonder how you parse this?
>
> I honestly thought until right now int
IL PROTECTED]
Sent: Tuesday, April 08, 2008 1:32 PM
To: python-list@python.org
Subject: Re: Best way to check if string is an integer?
hmmm
int() does miss some stuff:
>>> 1E+1
10.0
>>> int("1E+1")
Traceback (most recent call last):
File "", line 1, in
Valu
arg, as posted earlier:
int("10.0") fails, it will of course work with float("1E+1") sorry for
the noise...
On Tue, Apr 8, 2008 at 10:32 PM, Martin Marcher <[EMAIL PROTECTED]> wrote:
> hmmm
>
> int() does miss some stuff:
>
> >>> 1E+1
> 10.0
> >>> int("1E+1")
> Traceback (most recent call la
hmmm
int() does miss some stuff:
>>> 1E+1
10.0
>>> int("1E+1")
Traceback (most recent call last):
File "", line 1, in
ValueError: invalid literal for int() with base 10: '1E+1'
I wonder how you parse this?
I honestly thought until right now int() would understand that and
wanted to show that
> byte twiddling if the need arouse.
I'm excited already :)
** Posted from http://www.teranews.com **
--
http://mail.python.org/mailman/listinfo/python-list
Jorgen Grahn wrote:
> [0] There would have been more if Python had supported hexadecimal
> floating-point literals, like (I believe) C does.
C99 does. On the other hand, it isn't a feature I sorely missed during the
first 20 years or so of C's history, but you could always do some creative
byte t
On Sat, 5 Apr 2008 22:02:10 -0700 (PDT), Paddy <[EMAIL PROTECTED]> wrote:
> On Apr 6, 5:18 am, ernie <[EMAIL PROTECTED]> wrote:
>> On Apr 6, 10:23 am, Roy Smith <[EMAIL PROTECTED]> wrote:
>> > int(s) and catching any exception thrown just sounds like the best way.
>>
>> Another corner case: Is "5.
On Apr 6, 5:18 am, ernie <[EMAIL PROTECTED]> wrote:
> On Apr 6, 10:23 am, Roy Smith <[EMAIL PROTECTED]> wrote:
>
>
>
> > In article <[EMAIL PROTECTED]>,
> > Steve Holden <[EMAIL PROTECTED]> wrote:
>
> > > > This doesn't cater for negative integers.
>
> > > No, it doesn't, but
>
> > > s.isdigit() o
On Apr 6, 10:23 am, Roy Smith <[EMAIL PROTECTED]> wrote:
> In article <[EMAIL PROTECTED]>,
> Steve Holden <[EMAIL PROTECTED]> 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
In article <[EMAIL PROTECTED]>,
Steve Holden <[EMAIL PROTECTED]> 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(
John Machin wrote:
> On Apr 6, 9:25 am, Mark Dickinson <[EMAIL PROTECTED]> wrote:
>> On Apr 5, 6:19 pm, [EMAIL PROTECTED] wrote:
>>
>>> which is the best way to check if a string is an number or a char?
>>> could the 2nd example be very expensive timewise if i have to check a
>>> lot of strings?
>>
> I always do it the first way. It is simpler, and should be faster.
Ditto. Using int() is best. It's clear, it's correct, and it
should be as fast as it gets.
>> if c in '0123456789':
>>print "integer"
>> else:
>>print "char"
>
> Also, the second way will only work on single-digit num
On Apr 6, 9:25 am, Mark Dickinson <[EMAIL PROTECTED]> wrote:
> On Apr 5, 6:19 pm, [EMAIL PROTECTED] wrote:
>
> > which is the best way to check if a string is an number or a char?
> > could the 2nd example be very expensive timewise if i have to check a
> > lot of strings?
>
> You might be interest
On Apr 5, 6:19 pm, [EMAIL PROTECTED] wrote:
> which is the best way to check if a string is an number or a char?
> could the 2nd example be very expensive timewise if i have to check a
> lot of strings?
You might be interested in str.isdigit:
>>> print str.isdigit.__doc__
S.isdigit() -> bool
Ret
[EMAIL PROTECTED] wrote:
>which is the best way to check if a string is an number or a char?
>could the 2nd example be very expensive timewise if i have to check a
>lot of strings?
>
>this
>
>value = raw_input()
>
>try:
>value = int(value)
>except ValueError:
>print "value is not an intege
15 matches
Mail list logo