3

I get the following result:

>>> x = '-15'
>>> print x.isdigit()
False

When I expect it to be True. There seems to be no built in function that returns True for a string of negative number. What is the recommend to detect it?

Forethinker
  • 3,548
  • 4
  • 27
  • 48

4 Answers4

7

The recommended way would be to try it:

try:
    x = int(x)
except ValueError:
    print "{} is not an integer".format(x)

If you also expect decimal numbers, use float() instead of int().

Tim Pietzcker
  • 328,213
  • 58
  • 503
  • 561
  • 1
    @sasha.sochka: I'm not sure if the author wants `"123"` to be false. – Tim Pietzcker Jul 05 '13 at 21:11
  • 2
    I'm not sure OP necessarily wants negative numbers only. – miku Jul 05 '13 at 21:11
  • @arshajii: I can't think of any. Can you give me an example? – Tim Pietzcker Jul 05 '13 at 21:18
  • @sasha.sochka I am not sure what you meant, but Tim understood my question right. – Forethinker Jul 05 '13 at 21:19
  • @TimPietzcker your approach is exactly the way I thought of it. But I come from a school of thought that tries to avoid the usage of exception when possible. – Forethinker Jul 05 '13 at 21:20
  • @TimPietzcker Sorry, I meant the reverse. e.g. `int('+42')` works but `'+42'.isdigit()` returns `False`. Not sure if that matters here. – arshajii Jul 05 '13 at 21:23
  • 3
    @Forethinker: In Python, use of exceptions is explicitly encouraged. As miko linked to, it's easier to ask forgiveness than permission. It's even [faster in many cases](http://stackoverflow.com/a/1835844/20670) than an `if/else` construction. – Tim Pietzcker Jul 05 '13 at 21:23
  • 1
    @TimPietzcker the only situation I can think of is that as `str.isdigit()` is locale-dependant, it's possible that it'd return True for certain characters that `int()` wouldn't be able to work with... Other than that, I'm out of ideas as to Arshajii's statement – Jon Clements Jul 05 '13 at 21:23
  • @TimPietzcker Thanks for confirming the most pythonic way to solve this problem. – Forethinker Jul 05 '13 at 21:26
3

There might be a more elegant Python way, but a general method is to check if the first character is '-', and if so, call isdigit on the 2nd character onward.

Cannoliopsida
  • 3,044
  • 5
  • 36
  • 61
  • You want [`isdecimal`](http://stackoverflow.com/a/10604236/1763356) on Python 3 or on Python 2's `unicode`. – Veedrac Oct 10 '14 at 08:19
3

Maybe regex is an overhead here, but this could catch + and - before a number, and also could catch float and int as well:

(based on @Mark's comment)

CODE:

import re

def isdigit(string):
    return bool(re.match(r'[-+]?(?:\d+(?:\.\d*)?|\.\d+)', string))

DEMO:

print isdigit('12')       # True
print isdigit('-12')      # True
print isdigit('aa')       # False
print isdigit('1a2a')     # False
print isdigit('-12-12')   # False
print isdigit('-12.001')  # True
print isdigit('+12.001')  # True
print isdigit('.001')     # True
print isdigit('+.001')    # True
print isdigit('-.001')    # True
print isdigit('-.')       # False
Peter Varo
  • 11,726
  • 7
  • 55
  • 77
1

Use lstrip:

>>> negative_number = "-1"
>>> negative_number.lstrip('-').isnumeric()
True

>>> positive_number = "2"
>>> positive_number.lstrip('-').isnumeric()
True
Adriano
  • 750
  • 5
  • 9