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

2005-12-21 Thread [EMAIL PROTECTED]
How do I check if a string contains (can be converted to) an int? I want to do one thing if I am parsing and integer, and another if not. /David -- http://mail.python.org/mailman/listinfo/python-list

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

2005-12-21 Thread Erik Max Francis
[EMAIL PROTECTED] wrote: > How do I check if a string contains (can be converted to) an int? I > want to do one thing if I am parsing and integer, and another if not. try: x = int(aPossibleInt) ... do something with x ... except ValueError: ...

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

2005-12-21 Thread Steven D'Aprano
On Wed, 21 Dec 2005 02:12:35 -0800, [EMAIL PROTECTED] wrote: > How do I check if a string contains (can be converted to) an int? I > want to do one thing if I am parsing and integer, and another if not. Okay, this has got to be homework, surely. This is the third, maybe the fourth, question on th

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

2005-12-21 Thread Neuruss
Can't we just check if the string has digits? For example: >>> x = '15' >>> if x.isdigit(): print int(x)*3 45 >>> -- http://mail.python.org/mailman/listinfo/python-list

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

2005-12-21 Thread [EMAIL PROTECTED]
It's not homework in my case, I don't know about the others :) -- http://mail.python.org/mailman/listinfo/python-list

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

2005-12-21 Thread Juho Schultz
Neuruss wrote: > Can't we just check if the string has digits? > For example: > > x = '15' if x.isdigit(): > > print int(x)*3 > > > 45 > > No, we can't. '-15' has non-digits but is a valid int. Another point is that the try-except can also be used for string-to-float con

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

2005-12-21 Thread Steven D'Aprano
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.

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

2005-12-21 Thread Kent Johnson
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(...)

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

2005-12-21 Thread Paul Rubin
Kent Johnson <[EMAIL PROTECTED]> writes: > 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. Auh!! -- http://mail.python.org/mailman/listi

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

2005-12-21 Thread Antoon Pardon
Op 2005-12-21, Steven D'Aprano schreef <[EMAIL PROTECTED]>: > 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

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

2005-12-21 Thread Steven D'Aprano
On Wed, 21 Dec 2005 07:36:15 -0500, Kent Johnson wrote: > Maybe so, but '15'.isdigit() == True: Well I'll be a monkey's uncle. In that case, the name is misleadingly wrong. I suppose it is not likely that it could be changed before Python 3? -- Steven -- http://mail.python.org/mailman/listin

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

2005-12-21 Thread bonono
Steven D'Aprano wrote: > If you really wanted to waste CPU cycles, you could do this: > > s = "1579" > for c in s: > if not c.isdigit(): > print "Not an integer string" > break > else: > # if we get here, we didn't break > print "Integer %d" % int(s) > > > but notice th

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

2005-12-21 Thread Roy Smith
In article <[EMAIL PROTECTED]>, "[EMAIL PROTECTED]" <[EMAIL PROTECTED]> wrote: > How do I check if a string contains (can be converted to) an int? I > want to do one thing if I am parsing and integer, and another if not. > > /David The most straight-forward thing is to try converting it to an i

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

2005-12-21 Thread Peter Hansen
Steven D'Aprano wrote: > On Wed, 21 Dec 2005 07:36:15 -0500, Kent Johnson wrote: >>Maybe so, but '15'.isdigit() == True: > > Well I'll be a monkey's uncle. > > In that case, the name is misleadingly wrong. I suppose it is not likely > that it could be changed before Python 3? That was my first t

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

2005-12-21 Thread Steven D'Aprano
On Wed, 21 Dec 2005 05:15:23 -0800, bonono wrote: > > Steven D'Aprano wrote: >> If you really wanted to waste CPU cycles, you could do this: >> >> s = "1579" >> for c in s: >> if not c.isdigit(): >> print "Not an integer string" >> break >> else: >> # if we get here, we di

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

2005-12-21 Thread Daniel Schüle
[EMAIL PROTECTED] wrote: > How do I check if a string contains (can be converted to) an int? I > want to do one thing if I am parsing and integer, and another if not. > > /David > others already answered, this is just an idea >>> def isNumber(n): ... import re ... if re.match("^[-+]?[0

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

2005-12-21 Thread Steven D'Aprano
On Wed, 21 Dec 2005 16:39:19 +0100, Daniel Schüle wrote: > [EMAIL PROTECTED] wrote: >> How do I check if a string contains (can be converted to) an int? I >> want to do one thing if I am parsing and integer, and another if not. >> >> /David >> > > others already answered, this is just an idea >

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

2005-12-21 Thread Dave Hansen
On Thu, 22 Dec 2005 01:41:34 +1100 in comp.lang.python, Steven D'Aprano <[EMAIL PROTECTED]> wrote: [...] >Well, let's find out, shall we? [...] >A small but consistent speed advantage to the try...except block. > >Having said all that, the speed difference are absolutely trivial, less >than 0.1 mi

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

2005-12-21 Thread Alex Martelli
Erik Max Francis <[EMAIL PROTECTED]> wrote: > [EMAIL PROTECTED] wrote: > > > How do I check if a string contains (can be converted to) an int? I > > want to do one thing if I am parsing and integer, and another if not. > > try: > x = int(aPossibleInt) > ... do something

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

2005-12-21 Thread Erik Max Francis
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

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

2005-12-21 Thread Erik Max Francis
Steven D'Aprano wrote: > In that case, the name is misleadingly wrong. I suppose it is not likely > that it could be changed before Python 3? Why? The primary purpose of the .isdigit, etc. methods is to test whether a single character has a certain property. There is, however, no special char

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

2005-12-21 Thread Paul Rubin
Erik Max Francis <[EMAIL PROTECTED]> writes: > The primary purpose of the .isdigit, etc. methods is to test whether a > single character has a certain property. There is, however, no > special character data type in Python, and so by necessity those > methods must be on strings, not characters. R

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

2005-12-21 Thread David Rasmussen
Daniel Schüle wrote: > > others already answered, this is just an idea > I guess, if we want to avoid the exception paradigm for a particular problem, we could just do something like: def isNumber(n): try: dummy = int(n) return True except ValueError: return

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

2005-12-21 Thread Dave Hansen
On 21 Dec 2005 14:36:32 -0800 in comp.lang.python, Paul Rubin wrote: >There is a third choice which is the natural and obvious one: have the >function do what its name indicates. Return true if the arg is a >digit and false otherwise. If iterating over the whole strin

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

2005-12-22 Thread Peter Otten
Steven D'Aprano wrote: > On Wed, 21 Dec 2005 16:39:19 +0100, Daniel Schüle wrote: > >> [EMAIL PROTECTED] wrote: >>> How do I check if a string contains (can be converted to) an int? I >>> want to do one thing if I am parsing and integer, and another if not. >>> >>> /David >>> >> >> others alre

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

2005-12-22 Thread Grant Edwards
On 2005-12-21, Antoon Pardon <[EMAIL PROTECTED]> wrote: >> 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 But that is "obviously" wrong, since '15'

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

2005-12-22 Thread Steven D'Aprano
On Wed, 21 Dec 2005 13:58:01 -0800, Erik Max Francis wrote: > Steven D'Aprano wrote: > >> In that case, the name is misleadingly wrong. I suppose it is not likely >> that it could be changed before Python 3? > > Why? > > The primary purpose of the .isdigit, etc. methods is to test whether a >

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

2005-12-22 Thread Steven D'Aprano
On Thu, 22 Dec 2005 09:33:20 +0100, Peter Otten wrote: > Steven D'Aprano wrote: > >> On Wed, 21 Dec 2005 16:39:19 +0100, Daniel Schüle wrote: >> >>> [EMAIL PROTECTED] wrote: How do I check if a string contains (can be converted to) an int? I want to do one thing if I am parsing and int

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

2005-12-22 Thread Peter Otten
Steven D'Aprano wrote: > But since you're going to take my protests about regexes more seriously > than I intended you to, it is ironic that you supplied a regex that > is nice and fast but doesn't work: I think you said that "exceptions are cheap" elsewhere in this thread and I read your post ab

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

2005-12-23 Thread Fredrik Lundh
Grant Edwards wrote: > > So? the isdigit method tests whether all characters are digits. > > > '15'.isdigit() > > True > > But that is "obviously" wrong, since '15' is not a digit. no, but all characters in the string belongs to the "digit" character class, which is what the "is" predicates

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

2005-12-23 Thread Paul Rubin
"Fredrik Lundh" <[EMAIL PROTECTED]> writes: > no, but all characters in the string belongs to the "digit" character > class, which is what the "is" predicates look for. That description is not quite right. All characters in the empty string belong to the "digit" character class, but isdigit retur

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

2005-12-23 Thread Fredrik Lundh
Paul Rubin wrote: > That description is not quite right. All characters in the empty > string belong to the "digit" character class A: are there any blue cars on the street? B: no. not a single one. A: you're wrong! all cars on the street are blue! B: no, the street is empty.

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

2005-12-23 Thread Paul Rubin
"Fredrik Lundh" <[EMAIL PROTECTED]> writes: > A: are there any blue cars on the street? > B: no. not a single one. > A: you're wrong! all cars on the street are blue! B and A are both correct. It's just logic ;-). -- http://mail.python.org/mailman/listinfo/python-list

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

2005-12-23 Thread Duncan Booth
Fredrik Lundh wrote: > no, but all characters in the string belongs to the "digit" character > class, which is what the "is" predicates look for. > then gave examples including: "Life of Brian".istitle() > False I don't see how istitle() matches your definition of what the "is" predicate

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

2005-12-23 Thread Grant Edwards
On 2005-12-23, Fredrik Lundh <[EMAIL PROTECTED]> wrote: > Grant Edwards wrote: > >> > So? the isdigit method tests whether all characters are digits. >> > >> '15'.isdigit() >> > True >> >> But that is "obviously" wrong, since '15' is not a digit. > > no, but all characters in the string belong

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

2005-12-23 Thread Alex Martelli
Paul Rubin wrote: > "Fredrik Lundh" <[EMAIL PROTECTED]> writes: > > A: are there any blue cars on the street? > > B: no. not a single one. > > A: you're wrong! all cars on the street are blue! > > B and A are both correct. It's just logic ;-). Charles Lut